defaultTImezone and timezone

(Adinugro) #1

I have install php-intl so that the date and time can be localized to my country. I set the timeZone in common/config/main.php as follow,

return [
'aliases' => [
    '@bower' => '@vendor/bower-asset',
    '@npm' => '@vendor/npm-asset',
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'timeZone' => 'Asia/Jakarta',

in frontend/config/main.php, I have

‘formatter’ => [
‘class’ => ‘yii\i18n\Formatter’,
‘locale’ => ‘id-ID’,
‘defaultTimeZone’ => ‘Asia/Jakarta’,
‘nullDisplay’ => ‘’,
‘thousandSeparator’ => ‘.’,
‘decimalSeparator’ => ‘,’,
‘currencyCode’ => ‘Rp’

I save the datetime using

$model->stamp = date('Y-m-d H:i:s');

In my database it is save on mu timezone not UTC. However when I display it using

<?= Yii::$app->formatter->asDate($model->stamp 'php: j M y H:i:s'); ?>

I will have the stamp being displayed in 7 hour + the time of saved in database.

FYI, Jakarta is UTC +07:00

How could I correct this mistake?



(Pawel) #2

The usual way of configuration for timezones is to have defaultTimeZone left with default value (UTC) and timeZone to be set with your selected one. You can have the dates saved in DB in UTC and displayed back to you formatted in your timezone. Maybe try this.

Although I was sure that with defaultTimeZone and timeZone set to the same zone no shifting should be done, so I’m not sure what is going on there. Can someone confirm?