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.phpfile that there are separate types forDATE,DATETIMEandTIMEtype columns.With CakePHP 3.2 the
datetype doesn't mape toCake\I18n\Timeanymore, but to\Cake\I18n\Date(or\Cake\I18n\FrozenDatewhen instructed to use immutable objects), and it needs to be configured separately, which is why changing thedatetimetype, or the\Cake\I18n\Timeclass config won't affect yourDATEcolumns.To configure formatting for the latter, use the
\Cake\I18n\Dateand/or\Cake\I18n\FrozenDateclass and thedatetype. In your bootstrap, you could do something likeThat would override the defaults that are being applied when using the
pl_PLlocale. Note that you should useyyyyinstead ofYYYY(as the latter defines the week-numbering year), andMMinstead ofmm(as the latter defines minutes).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.
Also note that there's also
\Cake\I18n\Date::$wordFormatand\Cake\I18n\Date:$niceFormatwhich you may want to change too.See also