我想将 json 中的日期和时间格式与 laravel 分开

i want to separate Date and time format in json from laravel

你好吗?

我有 Laravel 项目和 json return 这样的日期

2019-05-04

和这样的时间 18:00:00

我怎样才能做到这样

年份:2019

月份:5 月

第 4 天

时间:06:00

timeS : 下午

我现在的代码是这样的

模特

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);

}

您没有将代码添加到问题中,所以我将尝试使用 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);

}

希望对您有所帮助。

另一种解决方案

您可以将 $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

Note that $record is the just an example name.

在此处阅读有关 Appending Values To JSON 的更多信息。