在编辑页面上显示相同的日期格式

Show the same date format on edit page

我用这种格式存储数据:

Carbon::createFromFormat('d F Y - H:i', $request->date);

在数据库中,日期字段是数据时间类型。

然后在编辑页面中,我会像这样显示存储的数据值:

value="{{ $rt->date }}

但它也出现了秒数。你知道如何显示相同的存储格式吗?

检索时格式化字段

{{ \Carbon\Carbon::parse($rt->date)->format('d/m/Y')}}

或来自docs

public function getDateAttribute($value)
{
    return \Carbon\Carbon::parse($value)->format('d/m/Y')
}

你可以做到

value="{{ $rt->date->format('d F Y - H:i') }}

要在获取 date 时始终获得准确的格式,您可以将其添加到您的模型中:

public function getDateAttribute($date)
{
    return $date->format('d F Y - H:i')
}

在模型上,您可以设置日期格式并强制使用 Carbon 的字段实例

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';    

    /**
    * The attributes that should be mutated to dates.
    *
    * @var array
    */
    protected $dates = ['deleted_at','date'];
}

如果您需要在 blade

上更改此格式
My flight date is {{$flight->date->format('d/m/Y')}}

试试这个

{{ \Carbon\Carbon::parse($rt->date)->format('d/m/Y')}}

它将日期转换为 'd/m/Y' 格式。您可以根据需要更改格式。