Cakephp 3.2 更改默认日期格式 [英] Cakephp 3.2 change default date format

查看:21
本文介绍了Cakephp 3.2 更改默认日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 CakePHP 3.2 的默认日期格式从 dd.mm.YYYY 设置为 YYYY-mm-dd,所以我不必使用 $MyDatas->mydate->format('Ym-d'),以及在编辑数据时表单中更重要的内容我将使用 dd.mm.YYYY(前 - 27.02.2016).我需要 YYYY-mm-dd (2016-02-27).

I try to set the default date format of CakePHP 3.2 from dd.mm.YYYY to YYYY-mm-dd, so I will not have to use $MyDatas->mydate->format('Y-m-d'), and what is more important in forms while editing data I will have date in format dd.mm.YYYY (ex- 27.02.2016). I need YYYY-mm-dd (2016-02-27).

我寻找了解决方案,但没有一个显示任何更改(在表单中或作为视图的一部分:$MyDatas->mydate):

I looked for solutions and none display any changes (in forms or as part of view: $MyDatas->mydate):

// in AppController

ini_set('intl.default_locale', 'pl_PL');

//and/or

use CakeDatabaseType;
Type::build('datetime')->useLocaleParser()->setLocaleFormat('YYYY-mm-dd');

//and/or

use CakeI18nI18n;
I18n::locale('pl_PL');

//and/or

use CakeI18nTime; 
Time::$defaultLocale = 'pl-PL'; //and or
Time::setToStringFormat('YYYY-mm-dd HH:mm');//and or
Type::build('datetime')->useLocaleParser(false);//and or

以上代码都没有帮助.有谁知道如何更改日期格式?

None of code above helped. Does anyone have any idea how I can change the date format?

推荐答案

我猜你已经升级到 CakePHP 3.2,否则你会在你的 config/bootstrap.php 文件中看到那里是 DATEDATETIMETIME 类型列的独立类型.

I guess you have been upgrading to CakePHP 3.2, otherwise you'd have seen in your config/bootstrap.php file that there are separate types for DATE, DATETIME and TIME type columns.

在 CakePHP 3.2 中,date 类型不再映射到 CakeI18nTime,而是映射到 CakeI18nDate(或 CakeI18nFrozenDate 当指示使用不可变对象时),并且需要单独配置,这就是为什么更改 datetime 类型,或 CakeI18nTime 类配置不会影响您的 DATE 列.

With CakePHP 3.2 the date type doesn't mape to CakeI18nTime anymore, but to CakeI18nDate (or CakeI18nFrozenDate when instructed to use immutable objects), and it needs to be configured separately, which is why changing the datetime type, or the CakeI18nTime class config won't affect your DATE columns.

要为后者配置格式,请使用 CakeI18nDate 和/或 CakeI18nFrozenDate 类和 date类型.在你的引导程序中,你可以做类似的事情

To configure formatting for the latter, use the CakeI18nDate and/or CakeI18nFrozenDate class and the date type. In your bootstrap, you could do something like

ini_set('intl.default_locale', 'pl_PL');

// ...

CakeI18nDate::setToStringFormat('yyyy-MM-dd');
CakeI18nFrozenDate::setToStringFormat('yyyy-MM-dd');

CakeDatabaseType::build('date')
    ->useImmutable()
    ->useLocaleParser()
    ->setLocaleFormat('yyyy-MM-dd');

这将覆盖使用 pl_PL 语言环境时应用的默认值.请注意,您应该使用 yyyy 而不是 YYYY(因为后者定义了 周编号年份),以及MM 而不是mm(后者定义分钟).

That would override the defaults that are being applied when using the pl_PL locale. Note that you should use yyyy instead of YYYY (as the latter defines the week-numbering year), and MM instead of mm (as the latter defines minutes).

https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax 用于 CakePHP 内部使用的 intl 格式化程序使用的格式.

See https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax for the format used by the intl formatter that CakePHP uses internally.

另请注意,还有 CakeI18nDate::$wordFormatCakeI18nDate:$niceFormat 您可能也想更改它们.

Also note that there's also CakeI18nDate::$wordFormat and CakeI18nDate:$niceFormat which you may want to change too.

另见

这篇关于Cakephp 3.2 更改默认日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆