Default Output Timezone


#1

I want to save all date/times in UTC and display for UK time (inc day light savings) so I leave:

date_default_timezone_set(‘UTC’);
ini_set(‘intl.default_locale’, Configure::read(‘App.defaultLocale’));
‘defaultLocale’ => env(‘APP_DEFAULT_LOCALE’, ‘en-GB’),

Is there a way to display all the date times with a global timezone and that would not affect them being saved in UTC. Can’t seem to see a setDefaultOutputTimezone() function, though it’s mentioned in the repo a while back.

Would be very handy as I can only see that I have to use the i18nFormat() function for each date that I want to show ajusted…

Please help, driving me bonkers.


#2

Set every locale config to UTC and then call setDefaultLocale

I have the following code in my bootstrap.php

// You should change this to UTC
date_default_timezone_set('America/Argentina/Buenos_Aires');

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

Cake\I18n\Time::setDefaultLocale($defaultLocale); // For any mutable DateTime
Cake\I18n\FrozenTime::setDefaultLocale($defaultLocale); // For any immutable DateTime
Cake\I18n\Date::setDefaultLocale($defaultLocale); // For any mutable Date
Cake\I18n\FrozenDate::setDefaultLocale($defaultLocale); // For any immutable Date

And set defaultLocale (in app) AND timezone(in datasources) in your app.php


#3

Thank you for the reply.

bootstrap.php:

date_default_timezone_set(‘UTC’);

$defaultLocale = Configure::read(‘App.defaultLocale’);
ini_set(‘intl.default_locale’, $defaultLocale);

Cake\I18n\Time::setDefaultLocale($defaultLocale); // For any mutable
DateTime
Cake\I18n\FrozenTime::setDefaultLocale($defaultLocale); // For any
immutable DateTime
Cake\I18n\Date::setDefaultLocale($defaultLocale); // For any mutable Date
Cake\I18n\FrozenDate::setDefaultLocale($defaultLocale); // For any
immutable Date

If I then debug the following on one of my views:

debug(\Cake\Core\Configure::read(‘App.defaultLocale’));
debug(date_default_timezone_get());
debug($note->created);
echo($note->created);

I get:

\src\Template\Element\case-tab-content.ctp (line 890)
‘en_GB’
\src\Template\Element\case-tab-content.ctp (line 891)
‘UTC’
\src\Template\Element\case-tab-content.ctp (line 892)
object(Cake\I18n\FrozenTime) {

‘time’ => ‘2017-09-13T09:34:12+00:00’,
‘timezone’ => ‘UTC’,
‘fixedNowTime’ => false

}
13/09/2017, 09:34

The note was created at 10:34 UK time. The output of the date doesn’t seem
to account for day light savings time. Though I believe that the date has
been stored correctly in UTC

Can you offer any further advice? You help is very much appreciated.

Thanks

Craig Deeming


#4

Are you getting the $note from a database? (I don’t know the time difference between GB and UTC)

Can you try what returns from nice or i18nFormat?

echo $note->created;
echo $note->created->nice();

Also can you try with en-GB instead of en_GB? (the examples use hypen)


#5

I am running WAMP on window, if that is of any issue.

Thanks

Craig Deeming


#6

Type::build(‘time’)
->useImmutable();
Type::build(‘date’)
->useImmutable();
Type::build(‘datetime’)
->useImmutable();

^ These are before the setlocale function. Does that effect anything?

Cake\I18n\Time::setDefaultLocale($defaultLocale);
Cake\I18n\FrozenTime::setDefaultLocale($defaultLocale);
Cake\I18n\Date::setDefaultLocale($defaultLocale);
Cake\I18n\FrozenDate::setDefaultLocale($defaultLocale);

Thanks

Craig Deeming