dev-x
dev-x

Reputation: 909

Datetime format is not same with database in Yii2

I want to make datetime format in my view in yii2 project is not same with datetime in my database. I use this code:

return Yii::$app->formatter->asDatetime($model->tanggal_sampai, "php:d M Y H:i");

The datetime in database is : 2016-06-14 16:53:40

But when I see the result of the code above in yii2, the result is not same. The result is : 14 Jun 2016 18:53

There is no problem with the date, but the time is very different. What's the problem? I use format 'WIB' because I'm in Indonesia.

Upvotes: 0

Views: 590

Answers (1)

user5201742
user5201742

Reputation:

In your main config file, put:

'components' => [
         'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:m/d/Y',
            'datetimeFormat' => 'php:Y-m-d H:i:s',
            'timeFormat' => 'php:H:i:s',
         ],

And you will get:

echo Yii::$app->formatter->asDatetime('2016-06-14 16:53:40');

2016-06-14 18:53:40

It takes the time as UTC, and adds +2 because of my Timezone. If you define date_default_timezone_set in index as UTC or Yii::$app->timeZone = 'UTC' it won't transform the time you've saved in your database.

2016-06-14 16:53:40

INFO: Timezones for Asia http://php.net/manual/en/timezones.asia.php

Upvotes: 1

Related Questions