GridView - timestamp column wrong date

Hi all. I have noticed with the data column in GridView when it has a timestamp value, the column isn’t displaying the right date value.

For instance, I have 2 columns with timestamp values, one named started_time and another named expiry_time. The values stored in the database for each one is the following:

Screenshot_20231201_151702

Taking as example the first one column, the date is 4 December 2023:

Screenshot 2023-12-01 at 15-19-36 Epoch Converter

But in the GridView I’m seeing the date has 1 day less:

Screenshot 2023-12-01 at 15-18-52 Códigos Promocionales

I have not set the time zone in the main config file but it doesn’t matter because the time doesn’t change even if I set the time zone to America/Bogota, anyway the server has such value in the php.ini config file so it isn’t important.

Why I’m getting such difference?

The first suspect here is timezone. How are you accounting for time zones?
see: Application, yii\base\Application | API Documentation for Yii 2.0 | Yii PHP Framework

Hi. It does matter and it is important. If it wouldn’t you haven’t had such property in the App config, but you have it. Get rid any custom timezone config from php.ini, as from your description you want to view date in UTC, right?

Why I’m getting such difference?

Because your server is set such way?

anyway the server has such value in the php.ini config (America/Bogota?)

If it’s not the case please show us your php config, the App config and your column view code snippet.

It’s a docker and in the environment variables for setup the php config file and the server config file, it has the time zone variable “TZ” in America/Bogota and the php date_timezone variable also in America/Bogota:

The GridView I have set as:

            <?= GridView::widget([
                'tableOptions' => [
                    'class' => 'table'
                ],
                'dataProvider' => $dataProvider,
                'filterModel' => $searchModel,
                'columns' => [
                    ['class' => 'yii\grid\SerialColumn'],
                    'id',
                    'code',
                    [
                        'attribute' => 'value',
                        'format' => ['currency', Yii::$app->formatter->currencyCode],
                    ],
                    'type',
                    [
                        'attribute' => 'uses',
                    ],
                    [
                        'attribute' => 'status',
                        'value' => function ($model) {
                                        return $model->status === 1 ? Yii::t('app', 'Active') : Yii::t('app', 'Inactive');
                                    },
                    ],
                    [
                        'attribute' => 'start_time',
                        'format' => ['date', "medium"],
                    ],
                    [
                        'attribute' => 'expiry_time',
                        'format' => ['date', "medium"],
                    ],
                    [
                        'class' => ActionColumn::className(),
                        'urlCreator' => function ($action, PromoCode $model, $key, $index, $column) {
                                        return Url::toRoute([$action, 'id' => $model->id]);
                                    }
                    ],
                ],
            ]); ?>

And in the config/main.php for backend, common and frontend I have set the timeZone:

<?php

return [
    ...
    'language' => 'es',
    'timeZone' => 'America/Bogota',
    'components' => [
        ...
    ]
];


UPDATE:
I have checked, it’s my bad. The format in unixtime I tried to save is for December 4th but I did not noticed the calculation was discounting 5 hours from the timezone.

1 Like