Set default timezone in Yii

梦想的初衷 提交于 2019-12-12 12:15:38

问题


I am using following config for timezone in config.php

'timeZone' => 'UTC',

It is working fine and all dates are stored in database according to UTC. Now each user has its own timezone in his/her profile like UTC+5, UTC-5, UTC+0, etc.

Now how can I show dates in reports according to user timezone. I used active record to fetch records from database. Is there any general way for all queries or we have to convert date to user timezone each time manually in php ??

Thanks.


回答1:


You can set Timezone in Yii by adding below value in main.php file of config

return array(   
  'timeZone' => 'Asia/Kolkata',
 )

For time zone list in PHP : http://php.net/manual/en/timezones.php




回答2:


You can use afterFind to change the time accordingly

public function afterFind()
{
    //apply your logic here
    $zone = Yii:app()->timeZone;

    // I don't know code to change time zone, so I assume there is a function named "functionToChangeTimeZone"
    $this->time = functionToChangeTimeZone($this->time , $zone);

    return parent::afterFind();
}

now every record will have there time zone changed according their settings




回答3:


After loading user data try:

Yii:app()->timeZone = $userTimezone;

where $userTimezone - is loaded user timezone from db. It's for using in Yii

OR you can set timezone in mysql ones by:

Yii::app()->db->createCommand('SET time_zone = <timezonename>')->execute()

and all TIMESTAMP fields will be in setted timezone for current session



来源:https://stackoverflow.com/questions/19763955/set-default-timezone-in-yii

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