Cakephp 3.2更改默认日期格式 [英] Cakephp 3.2 change default date format
问题描述
我尝试将CakePHP 3.2的默认日期格式从 dd.mm.YYYY
设置为 YYYY-mm-dd
,所以我不必使用 $ MyDatas-> mydate-> format('Ym-d')
数据我将日期格式为 dd.mm.YYYY
(ex-27.02.2016)。我需要 YYYY-mm-dd
(2016-02-27)。
我寻找解决方案,任何更改(以形式或作为视图的一部分: $ MyDatas-> mydate
):
<
// in AppController
ini_set('intl.default_locale','pl_PL');
//和/或
使用Cake \Database\Type;
Type :: build('datetime') - > useLocaleParser() - > setLocaleFormat('YYYY-mm-dd');
//和/或
使用Cake \I18n\I18n;
I18n :: locale('pl_PL');
//和/或
使用Cake \I18n\Time;
Time :: $ defaultLocale ='pl-PL'; // and or
Time :: setToStringFormat('YYYY-mm-dd HH:mm'); //和或
Type :: build('datetime') - > useLocaleParser // and或
上面的代码都没有帮助。有没有人知道如何更改日期格式?
在 config / bootstrap.php
文件中看到有 DATE
, DATETIME
和 TIME
类型列。
$ c> date 类型不再映射到 Cake\I18n\Time
,而是到 \Cake \I18n\Date
(或 \Cake\I18n\FrozenDate
时指示使用不可变对象),它需要单独配置,这就是为什么要更改 datetime
类型或 \Cake\I18n\Time
类配置不会影响您的 DATE
列。
要为后者配置格式,请使用 \Cake\I18n\Date
和/或 \Cake\I18n\FrozenDate
类和 date
类型。在你的引导,你可以做一些像
ini_set('intl.default_locale','pl_PL');
// ...
Cake\I18n\Date :: setToStringFormat('yyyy-MM-dd');
Cake\I18n\FrozenDate :: setToStringFormat('yyyy-MM-dd');
\Cake\Database\Type :: build('date')
- > useImmutable()
- > useLocaleParser()
- > setLocaleFormat('yyyy-MM-dd);
这将覆盖使用 pl_PL
locale。请注意,您应该使用 yyyy
而不是 YYYY
(因为后者定义 week-numbering year )和 MM
而不是
还要注意,还有 \Cake\I18n\Date :: $ wordFormat
和 \Cake\I18n\Date:$ niceFormat
,您也可以更改。
另请参阅
- 源> cakephp / app> config / bootstrap.php
- API> \Cake\I18n\ DateFormatTrait :: i18nFormat()
- API> \Cake\I18n\Date :: $ wordFormat
- API> \Cake\I18n\Date :: $ niceFormat
- Cookbook> Date&时间>不可更改的日期和时间
- 食谱>国际化&本地化>解析本地日期时间数据
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).
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 Cake\Database\Type;
Type::build('datetime')->useLocaleParser()->setLocaleFormat('YYYY-mm-dd');
//and/or
use Cake\I18n\I18n;
I18n::locale('pl_PL');
//and/or
use Cake\I18n\Time;
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?
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.
With CakePHP 3.2 the date
type doesn't mape to Cake\I18n\Time
anymore, but to \Cake\I18n\Date
(or \Cake\I18n\FrozenDate
when instructed to use immutable objects), and it needs to be configured separately, which is why changing the datetime
type, or the \Cake\I18n\Time
class config won't affect your DATE
columns.
To configure formatting for the latter, use the \Cake\I18n\Date
and/or \Cake\I18n\FrozenDate
class and the date
type. In your bootstrap, you could do something like
ini_set('intl.default_locale', 'pl_PL');
// ...
Cake\I18n\Date::setToStringFormat('yyyy-MM-dd');
Cake\I18n\FrozenDate::setToStringFormat('yyyy-MM-dd');
\Cake\Database\Type::build('date')
->useImmutable()
->useLocaleParser()
->setLocaleFormat('yyyy-MM-dd');
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).
See http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details for the format used by the intl formatter that CakePHP uses internally.
Also note that there's also \Cake\I18n\Date::$wordFormat
and \Cake\I18n\Date:$niceFormat
which you may want to change too.
See also
- Source > cakephp/app > config/bootstrap.php
- API > \Cake\I18n\DateFormatTrait::i18nFormat()
- API > \Cake\I18n\Date::$wordFormat
- API > \Cake\I18n\Date::$niceFormat
- Cookbook > Date & Time > Immutable Dates and Times
- Cookbook > Internationalization & Localization > Parsing Localized Datetime Data
这篇关于Cakephp 3.2更改默认日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!