我想从laravel中分离json中的日期和时间格式

时间:2019-07-11 19:10:01

标签: json laravel api laravel-5.8

你好吗?

我有Laravel项目和json返回日期

2019-05-04

和这样的时间 18:00:00

我怎么能这样

年:2019

month:5月

日期:04

时间:06:00

timeS:PM

我的代码现在是这个

模型

public $table = 'bookings';

protected $dates = [
    'date',
    'created_at',
    'updated_at',
    'deleted_at',
];

protected $fillable = [
    'date',
    'time',
    'user_id',
    'type_id',
    'persons',
    'order_no',
    'created_at',
    'updated_at',
    'deleted_at',
    'table_cat_id',
    'booking_status_id',
];

控制器是

public function index(Request $request)
{
    $user = Auth::user();
    if(isset($user->id)){
        $bookings = Booking::where('user_id', $user->id)->get();
    }else{
        $bookings = null;
    }
    return response()->json($bookings);

}

2 个答案:

答案 0 :(得分:1)

您尚未将代码添加到问题中,因此我将尝试使用carbon提供答案:

//add this line to your controller:
use Carbon\Carbon;

//Parse the date you want to use:
$date = Carbon::parse($date); //Or use Carbon::now() to get the current time

$year  = $date->year;
$month = $date->format('F');
$day   = $date->day;
$timeS = $date->format('A');
$time  = $date->format('H:i');

//now return your json:

return response()->json([
    'year'  => $year,
    'month' => $month,
    'day'   => $day,
    'timeS' => $timeS,
    'time'  => $time,
]);

更新 要使用您的代码,请执行以下操作: 选择您要使用的日期,例如created_atupdated_at。 然后,执行以下操作:

//add this line to your controller:
use Carbon\Carbon;

public function index(Request $request)
{
    $user = Auth::user();
    if(isset($user->id)){
        $bookings = Booking::where('user_id', $user->id)->get();

        //With the created_at field:
        foreach($bookings as $booking){
            $date  = Carbon::parse($booking->created_at);
            $year  = $date->year;
            $month = $date->format('F');
            $day   = $date->day;
            $timeS = $date->format('A');
            $time  = $date->format('H:i');

            $dates = array(
                'year'  => $year, 
                'month' => $month, 
                'day'   => $day, 
                'timeS' => $timeS, 
                'time'  => $time
            );

            $booking->dates = $dates;
        }
    }else{
        $bookings = null;
    }
    return response()->json($bookings);

}

希望有帮助。

答案 1 :(得分:0)

另一种解决方案

您可以将$dates附加到模型中。

要执行此操作,请按照以下步骤操作:

  1. $appends添加到您的模型中:
protected $appends = ['dates'];
  1. 在模型中使用Carbon:
use Carbon\Carbon;
  1. 在模型中创建一个getDatesAttribute函数(它是访问器):
public function getDatesAttribute()
{
    $date = Carbon::parse($this->created_at) //You can use any date field you want

    $year  = $date->year;
    $month = $date->format('F');
    $day   = $date->day;
    $timeS = $date->format('A');
    $time  = $date->format('H:i');

    return array (
        'year'  => $year, 
        'month' => $month, 
        'day'   => $day, 
        'timeS' => $timeS, 
        'time'  => $time
    );

}
  1. 现在,每次使用模型执行查询时,dates将包含在返回的集合中。您可以通过以下方式访问它:
$record->dates
  

请注意,$record仅是示例名称。

在此处详细了解Appending Values To JSON