Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default timezone in Yii

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.

like image 653
Awan Avatar asked Jan 13 '23 00:01

Awan


2 Answers

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

like image 157
Maulik Bhojani Avatar answered Jan 16 '23 20:01

Maulik Bhojani


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

like image 29
Developerium Avatar answered Jan 16 '23 21:01

Developerium