Set default timezone in Yii

12,675

Solution 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

Solution 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

Solution 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

Share:
12,675
Awan
Author by

Awan

Upvoter

Updated on June 18, 2022

Comments

  • Awan
    Awan almost 2 years

    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.