Formatting Date time in view in CakePHP 3

I’m using CakePHP 3.4.

I have set my database timezone to UTC. I am from IST Timezone.

To display DateTime in IST Timezone, this is what I’m doing.

<?= $this->Time->format($user->created, \IntlDateFormatter::FULL, null, 'Asia/Kolkata') ?>

and this is Displaying Time as

Sunday, May 28, 2017 at 2:56:21 PM India Standard Time

I want to display Date in below format

May 28, 2017 2:56:21 PM IST

How I can achieve this?

you need set locale so cake change ur date / currency display for all app:

in config/app.php
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'UR_LOCALE_CODE),

in config/bootstrap.php (u must change format for your locale)

 //Enable immutable time objects in the ORM.
 //
 //You can enable default locale format parsing by adding calls
 //to `useLocaleParser()`. This enables the automatic conversion of
 //locale specific date formats. For details see
 //@link http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data

//Change the preferred class name to the FrozenTime implementation. 
\Cake\Database\Type::build('time')->useImmutable();
\Cake\Database\Type::build('date')->useImmutable();
\Cake\Database\Type::build('datetime')->useImmutable();
\Cake\Database\Type::build('timestamp')->useImmutable();


//http://discourse.cakephp.org/t/cakephp-locale-pt-br-and-dateformat/1002
//https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#localizing-dates-and-numbers
//https://book.cakephp.org/3.0/en/core-libraries/time.html#setting-the-default-locale-and-format-string
//format chronos
\Cake\I18n\Time::setToStringFormat([IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT]);
\Cake\I18n\Time::setToStringFormat('dd/MM/YYYY HH:mm');
\Cake\I18n\Date::setToStringFormat('dd/MM/yyyy');
\Cake\I18n\FrozenTime::setToStringFormat('dd/MM/yyyy HH:mm');
\Cake\I18n\FrozenDate::setToStringFormat('dd/MM/yyyy');

//https://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#localizing-dates-and-numbers

\Cake\Database\Type::build('date')->useLocaleParser()->setLocaleFormat('dd/MM/yyyy');
\Cake\Database\Type::build('datetime')->useLocaleParser()->setLocaleFormat('dd/MM/yyyy HH:mm');
\Cake\Database\Type::build('timestamp')->useLocaleParser()->setLocaleFormat('dd/MM/yyyy HH:mm');

\Cake\Database\Type::build('decimal')->useLocaleParser();
\Cake\Database\Type::build('float')->useLocaleParser();

Can you tell me that what will be the value for IST(Indian Standard time)?
in config/app.php
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'UR_LOCALE_CODE),

@Diego there’s a difference beween locale and timezone. Your suggestion doesn’t work, since the TO wants a different Time format.

@anuj9196

\IntlDateFormatter::FULL works exactly this way. See http://php.net/manual/en/class.intldateformatter.php

“May 28, 2017 2:56:21 PM IST” is a custom time format.

Try

$user->created->i18nFormat('MMM dd, yyyy h:mm:ss a z')

See http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details for a full list of options.

1 Like