default date formats cakephp 4
how to print dates in your language in a cakephp view First of all imagine you have a datetime field in your table, tipically we have a created and modified field in our table. The lazy programmer ...
h < 1>how to print dates in your language in a cakephp view
First of all imagine you have a datetime field in your table, tipically we have a created and modified field in our table.
The lazy programmer prints just this:
<?= $table->modified ?>
And he will get
2020-12-09 18:49
Not so user-friendly.
The time helper
You can use the great TimeHelper in your views to get the date correctly formatted
<?= $this->Time->nice($reservation->modified) ?>
so you wil get
Dec 9th, 2020 - 6:49PM
Print the date in your language
In case you are an Italian programmer, and you want the dates in your language, you should define a default locale in your config/bootstrap.php
// put this in your config/bootstap.php
use Cake\I18n\FrozenDate;
use Cake\I18n\FrozenTime;
use Cake\I18n\Time;
use Cake\I18n\Date;
Time::setDefaultLocale('it-IT'); // For any mutable DateTime
FrozenTime::setDefaultLocale('it-IT'); // For any immutable DateTime
Date::setDefaultLocale('it-IT'); // For any mutable Date
FrozenDate::setDefaultLocale('it-IT'); // For any immutable Date
After this, your nice date will be in Italian along all the program
9 dic 2020, 18:49