我有一个表格,我通过日期选择器以日期格式存储数据。在存储日期时我正在做这样的事情:
$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
我检查了 mySQL 数据库的格式,我有这样的日期:
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 中缺少数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880705/