Adobe
Adobe

Reputation: 568

Wrong timezone in GridView yii2

Hello I have some trouble with timezone. My timeZone is Asia/Novosibirsk.

In index.php set timezone and yii formatter output:

\Yii::$app->formatter->timeZone // Asia/Novosibirsk

So in yii\grid\GridView winget I have the next columns of the same date (date_add):

            [
              'attribute' => 'date_add',
              'format' => ['date', 'full']
            ],
            'date_add:datetime',
            [
              'attribute' => 'date_add',
              'value' => function($row){
                return \Yii::$app->formatter->asDatetime(new DateTime($row->date_add), 'full');
              }
            ],

Output different values: enter image description here

Only third column is right, 14 December (raw date is 2016-12-14 17:00:00).

I assume, that GridView have own timeZone (or formatter class instance)

Upvotes: 1

Views: 2177

Answers (1)

Bizley
Bizley

Reputation: 18021

GridView is using formatter component (you can change it's settings in GridView or for the whole application in the application configuration) so just modify the time zone and everything should work properly.

For just the GridView settings:

<?= GridView::widget([
    // ...
    'formatter' => [
        'class' => 'yii\i18n\Formatter',
        'timeZone' => 'Asia/Novosibirsk'
    ],
    // ...
]) ?>

For the whole application (configuration file like config/web.php in basic project template, settings for formatter):

return [
    // ...
    'components' => [
        // ...
        'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'timeZone' => 'Asia/Novosibirsk'
        ],
    ],
];

or application wide:

return [
    // ...
    'timeZone' => 'Asia/Novosibirsk'
    // ...
];

Upvotes: 1

Related Questions