编程语言
首页 > 编程语言> > php – 无法找到两位数的月份Laravel中缺少数据

php – 无法找到两位数的月份Laravel中缺少数据

作者:互联网

我有一个表格,我通过datepicker以日期格式存储数据.在存储日期时我正在做这样的事情:

$data['schedule'] = Carbon::parse($request->schedule)->toDateTimeString();

然后在模型中我定义如下:

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

现在在检索日期时我正在使用diffForHumans(),如下所示:

$event->schedule = $event->schedule->diffForHumans();

但它抛出这样的错误:

A two digit month could not be found. Data missing

我检查了我的SQL数据库格式,我有这样的日期:

2017-07-02 14:38:23

编辑:

以前我在表中存储日期时遇到问题,详细说明请参考How to format date receiving through Vuejs Datepicker in laravel.它抛出错误:

Invalid datetime format: 1292 Incorrect datetime value: ‘2017-05-04T18:30:00.000Z’ for column ‘schedule’ at row 1

所以我选择在将它存储在数据库中时使用Carbon :: parse.

如何解决这个问题,帮助我.

谢谢.

解决方法:

尝试使用模型中的存取方法.

public function getScheduleAttribute($value)
{
    return Carbon::parse($value)->diffForHumans();
}

并将该属性作为

$event->schedule;

标签:php,laravel,laravel-5-4,php-carbon
来源: https://codeday.me/bug/20190608/1196637.html