CakePHP 4.x: Change defaultLocale but the date format not change

Hi everyone,
I would like to change the date format in in form from mm/dd/yyyy to dd/mm/yyyy. I changed my defaultLocate from en_US to vi_VN in app.php (i read the document in link: Internationalization & Localization - 4.x). But the date format not change. Please help me a solution.
Thank you very much.

It indeed does change the format, it just doesn’t change the format to the one you expect. Try e.g. setting en_US and then de_DE.
See Date & Time - 4.x

You can set your own custom date format in your config/bootstrap.php at the end via the following code

$format = 'dd.MM.yyyy HH:mm';

\Cake\I18n\FrozenTime::setToStringFormat( $format );  // For any immutable DateTime
\Cake\I18n\FrozenDate::setToStringFormat( $format );  // For any immutable Date

Be aware, that this format is NOT the default PHP dateformat, its instead the ICU format.

1 Like

Many thanks Kenvin . All view format are changed. But my date field control in form is not change the format (pls see the attachment image).

CakePHP generates <input type="date"> fields which depend on either

  • your browser locale settings or
  • your OS locale settings

See e.g. Chrome: Quick FAQs on input[type=date] in Google Chrome  |  Web  |  Google Developers

Enforcing a specific dateformat input is not something that CakePHP handles by default. You will have to add that yourself via something like a JS library like FlatPickr

1 Like

Finally it s working. The code is bellow:

<?php echo $this->Form->control('startdate', ['empty' => true, 'type' => 'text']);?>

// javascript code

$(function () {
        enableTime: false,
        dateFormat: "d/m/Y"

Thank a lot Kenvin.

Seems it uses ICU message format for dates. You can try different date formats in this ICU editor and changing the language to see how it formats the date for different languages.