Issue in yii2 timezone formatter

梦想与她 提交于 2019-12-07 03:40:15

问题


in php.ini timezone is UTC. system timezone is UTC. yii defaultTimeZone is UTC. But my datetime attribute gets converted to my timezone "Asia/Kolkata" before saving into db.

Eg: UTC time 12:00Hrs my input 17.30hrs what I expect in db is 12:00hrs and in view 17.30hrs But what I got in db is 17:30hrs and in view I got 23:00hrs.

web.php:

'formatter' => 
        [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:d-m-Y',
            'datetimeFormat' => 'php:d-m-Y H:i a',
            'timeFormat' => 'php:H:i A',
            'timeZone' => 'Asia/Kolkata',
        ],

回答1:


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

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 for display for your locale. You can set something like this in your config file that you can access across

'formatter' => 
        [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:d-m-Y',
            'datetimeFormat' => 'php:d-m-Y 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 refer Doc.

Hope its works.



来源:https://stackoverflow.com/questions/33139434/issue-in-yii2-timezone-formatter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!