Formatter is adding one hour on time

Hi,

I have in my database the field created_at with value : 1610752823 (timestamp).

When i run yii2 formatter to show in timezone “America/Sao_Paulo” (like the image up) it is showing with plus one hour:

Can anyone help me?

I made a simple sample without yii2, with pure php:

echo 'DATE (America/Sao_Paulo) = '.date("Y-m-d H:i:s", 1610752823);

And it show correct:

DATE (America/Sao_Paulo) = 2021-01-15 20:20:23

But with yii2 it is adding one hour:

<?= Yii::$app->formatter->asDatetime($model->created_at) ?>

Example:

<?= Yii::$app->formatter->asDatetime(1610752823) ?>

Show: 15 de jan de 2021 21:20:23

I already add timezone America/Sao_Paulo to php.ini.

I var_dump formatter object and it is configured:

 ["locale"]=> string(5) "pt-BR" ["language"]=> string(5) "pt-BR" ["timeZone"]=> string(17) "America/Sao_Paulo"

But still with problem.

I’m also experiencing this issue :cry:
Did you find the solution? @prchakal

Just found the solution. You have to add in web.php config like this:

components' => [
        'formatter' => [
            'defaultTimeZone' => 'America/Sao_Paulo',
       ],
       ...

Did you set the application’s timezone property in your application config? Together with ‘language’ should be enough to solve all problems with date formatting.
https://www.yiiframework.com/doc/api/2.0/yii-base-application#$timeZone-detail

Setting it could get you in trouble at some point:
https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#$defaultTimeZone-detail

It defaults to UTC so you only have to adjust this value if you store datetime values in another time zone in your database.

1 Like