laravel无法识别ISO8601格式的日期时间

问题描述 投票:0回答:2

假设我们有一个来自angularJs客户端应用程序的2015-02-05T14:50:55+01:00格式化日期时间。

现在我想将它保存为表中的DATETIME MySQL。

我知道这是一个iso 8601格式的日期时间。因此,我将以下代码添加到我的模型中:

protected $dateFormat = 'DATE_ISO8601';

但是当我想用这个新字段更新我的模型时,我收到了这个错误:

A textual day could not be found
Meridian can only come after an hour has been found
The format separator does not match
The format separator does not match
The format separator does not match
The timezone could not be found in the database
Data missing {"userId":1,"email":"[email protected]","exception":"[object] (InvalidArgumentException(code: 0): A textual day could not be found
Meridian can only come after an hour has been found
The format separator does not match
The format separator does not match
The format separator does not match
The timezone could not be found in the database
Data missing at D:\\wamp\\www\\zarsystem\\vendor\
esbot\\carbon\\src\\Carbon\\Carbon.php:582)
[stacktrace]
#0 D:\\wamp\\www\\zarsystem\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php(715): Carbon\\Carbon::createFromFormat('DATE_ISO8601', '2015-02-05T14:5...')

我尝试了一些其他的预定义常量,如qazxsw poi,qazxsw poi,......但是也有同样的错误。

我很困惑,我不知道该怎么办?

php mysql angularjs laravel laravel-5.5
2个回答
1
投票

你可以这样做:

DATE_ATOM

DATE_W3C是ISO 8601日期。

protected $dateFormat = 'c';

此外,我相信这不是你想要的,因为你之前说过你想在数据库中以c的标准格式保留日期。

所以,你需要添加http://php.net/manual/en/function.date.php来转换日期,然后从DB和Y-m-d H:i:s获取它,然后再将它从an accessor转换回a mutator,然后再保存到DB。

默认情况下,时间戳格式为'Y-m-d H:i:s'。如果需要自定义时间戳格式,请在模型上设置$ dateFormat属性。此属性确定数据库中日期属性的存储方式,以及将模型序列化为数组或JSON时的格式

ISO 8601


0
投票

Y-m-d H:i:s是一个常量,在https://laravel.com/docs/5.5/eloquent-mutators#date-mutators类中定义,你试图使用DATE_ISO8601的格式掩码字符串

你应该能做到的

DateTime

但是,这种格式与ISO-8601不兼容,但出于向后兼容的原因,这种格式是这样的。所以最好使用ATOM常量

'DATE_ISO8601'
© www.soinside.com 2019 - 2024. All rights reserved.