Problem in formatting yii2 time zones

in the timezone php.ini is UTC. system time zone is UTC. yii defaultTimeZone - UTC. But my datetime attribute is converted to my Asia / Calcutta timezone before being stored in db.

For example: UTC time 12: 00Hrs my entry 17:30 what I expect in db is 12: 00hrs and, according to 17:30, but what I got in dB is 17: 30 hours, and apparently I get 23:00.

web.php:

'formatter' => [ 'class' => 'yii\i18n\Formatter', 'dateFormat' => 'php:dm-Y', 'datetimeFormat' => 'php:dmY H:i a', 'timeFormat' => 'php:H:i A', 'timeZone' => 'Asia/Kolkata', ], 
+6
source share
1 answer

You can save a specific timestamp value using a predefined format. So let's assume that you have defined your datetime field in the backend as INTEGER, and you want to save it as an integer. You can set this behavior as

 public function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time', ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time', ], 'value' => function() { return date('U'); // unix timestamp }, ], ]; } 

You can configure yii \ i18n \ formatter to control your global date formats to display for your locale. You can set something like this in your configuration file, with which you can access through

 'formatter' => [ 'class' => 'yii\i18n\Formatter', 'dateFormat' => 'php:dm-Y', 'datetimeFormat' => 'php:dmY H:i a', 'timeFormat' => 'php:H:i A', 'defaultTimeZone' OR 'timeZone' => 'Asia/Calcutta', //global date formats for display for your locale. ], 

Read this link and also Doc .

We hope for his work.

+1
source

All Articles