2

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?

John Smith
  • 7,243
  • 6
  • 49
  • 61
MasterAbe
  • 23
  • 1
  • 1
  • 3

3 Answers3

8

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 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::$wordFormat and \Cake\I18n\Date:$niceFormat which you may want to change too.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110
  • I clean my AppController.php from my code. I add to bootstrap.php your solution and it did not work. However I changed your Type::build('date') rule by removing ->useImmutable(): Type::build('date')->useLocaleParser()->setLocaleFormat('YYYY-MM-dd'); and it worked! THX – MasterAbe Feb 28 '16 at 08:42
  • @MasterAbe `useImmutable()` shouldn't affect parsing/formatting in any way. – ndm Feb 28 '16 at 08:52
  • but it does... removing it formats date like i need to, but just adding it will not make change in date format. I tested it in bootstrap and in AppController. – MasterAbe Feb 28 '16 at 09:31
  • @MasterAbe Sorry, forgot that `\Cake\I18n\FrozenDate` and `Cake\I18n\Date` do not share the same config. I've updated my answer. – ndm Feb 28 '16 at 10:40
  • I did all these configs and it's not working. When I echo in my controller the data is correct: 15/12/2016. But when it goes to the validation rule it says that is an invalid date. I did the configs like you guys said but only changing the format to dd/MM/yyyy. Still not working :( Any solutions? Do I have to parse the string to transform it in date type before goes to the rule validation? – Victor Dec 15 '16 at 13:08
  • @Victor Formatting/Parsing and validation are separate things, the latter isn't affected by global built-in configuration: **http://stackoverflow.com/q/34896273/1392379** – ndm Dec 15 '16 at 16:21
4

after a lot of searching I found.

In the config/app.php under the 'App' => []

change

'defaultLocale' => env('APP_DEFAULT_LOCALE', 'us_US'),

to

'defaultLocale' => env('APP_DEFAULT_LOCALE', 'pl_PL'),

and all your date will change to your formatting. because in config/boostrap.php it read

ini_set('intl.default_locale', Configure::read('App.defaultLocale'));

end in the cake\I18n FrozenDate it set to

protected static $_toStringFormat = [IntlDateFormatter::SHORT, -1];
Lean Pilar
  • 327
  • 7
  • 13
0

Paste this code in this file config/site.php:

return [
 'Site' => [      
        'CakeDateFormat' => 'Y-M-d',
        'DatePickerFormat' => 'mm/dd/yyyy',
        'CakeDateFormatForView' => 'm/d/Y',        
    ]
]

Use this date format in controller:

use Cake\I18n\Time;

$this->request->data['date'] =Time::parseDate($this->request->data['date'], Configure::read('Site.CakeDateFormat'));

Set the date format according to your needs in CakeDateFormat.

IanB
  • 3,489
  • 1
  • 20
  • 24