简体   繁体   English

CakePHP 3日期格式问题

[英]CakePHP 3 Date format Issue

I researched everywhere but could not find the solution, 我到处研究,但找不到解决方案,
In the database, I am using from_date field as date (2018-07-07) type But when I find the data then it auto converts in from_date to 'Cake\\I18n\\Date Object' 在数据库中,我将from_date字段用作date (2018-07-07)类型,但是当我找到数据时,它将自动将from_date转换为'Cake \\ I18n \\ Date Object'

[from_date] => Cake\I18n\Date Object
        (
            [time] => 2018-07-07T00:00:00+00:00
            [timezone] => UTC
            [fixedNowTime] => 
        )

I what this as 我这是什么

[from_date] => '2018-07-07'

I know we can covert this by 我知道我们可以隐瞒

$obj->from_date->format('Y-M-D');

but the data is coming in an array of 10000 loops, this is not a good idea to convert in the loop. 但是数据要以10000个循环的数组的形式传入,因此在循环中进行转换不是一个好主意。 I also tried this in bootstrap.php 我也在bootstrap.php中尝试过

Type::build('date')->useLocaleParser()->setLocaleFormat('y-m-d');

but this is also not working. 但这也不起作用。

Please help 请帮忙

in your bootstrap.php you can set the default format you want when converting Date and FrozenDate to string in Json bootstrap.php ,可以设置将DateFrozenDate转换为Json中的字符串时所需的默认格式

see the manual here and the api here 请参阅手册在这里和API 这里

note that you have to use string patterns described here and not the patterns you usually use with date() 请注意,您必须使用此处描述的字符串模式而不是通常与date()使用的模式。

so your code should be 所以你的代码应该是

\Cake\I18n\FrozenDate::setJsonEncodeFormat('yyyy-MM-dd'); 
\Cake\I18n\Date::setJsonEncodeFormat('yyyy-MM-dd'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM