ive been reading the forms in search for a solution to a timezone question i have to no avail;

i have an application that needs to support multiple timezones

say i have a user that is planning an event, he is in ariona(gmt-‘7’), he plans the event and is stored in the db the date/time of the event is stored in a timezone type field time that is stored is 2011-01-20 21:00:00

someone from California(gmt-‘8’) wants to join this event, but we need to see if the event is expired so he can join.

from what ive read i can just change the sql timezone via


then all date calls will be relative to the users timezone

so if i login as the ca user and its 2011-01-20 21:00:00

and i goto join the event when

$event= event::model()->eventtime;

$currendatetime=date(Y-m-d H:i:s);

if($event > $currentdatetime ==true)

echo 'true'; //here we goto a diff function 

if this is true then where would the best place to put the following code be.


i had it in the main controller but i know there has to be a better place, but wouldnt i need to set the timezone with each page request in order to keep this relative. this only hast to keep timezone tracking for logged in users.

to put it under onBeginRequest event would be a good idea

if you have never used it here is how you do it

in the main config

return array(

//your config



and then create and import it

class MyBootstrap{

	public static function myBeginRequestMethod(){

   	//set the timezone here



ok i did that and can verify its working like

    public static function setTimezone(){

        echo 'default timezone is <br>';


        echo $server_tz .'<br>';

        echo 'date(\'Y-m-d h:i:s\') is ' . date('Y-m-d h:i:s'). '<br>';




        echo 'current timezone is <br>';

        echo $curr_tz.'<br>';


        echo 'date(\'Y-m-d h:i:s\') is ' . date('Y-m-d h:i:s'). '<br>';


output is 

default timezone is


date('Y-m-d h:i:s') is 2011-01-23 12:16:11

current timezone is


date('Y-m-d h:i:s') is 2011-01-23 02:16:11

so echo datetime displays the correct time, however if i enter a record as the lgged in user, then retrieve it as same user and vardump/echo the datetime field

it still shows the time relative to the server set timezone and not the user set timezone.

im storing timestamp in my model as as such

public function beforeSave() 


	if ($this->isNewRecord) 

	$this->entered = new CDbExpression('NOW()');



	return parent::beforeSave();


looking at it via phpmyadmin shows incorrect timestamp as well, why is this, how do i fix

Hi Mithereal.

I’m looking for some answers too. Did you solve the above?

If I understand it correctly, date_timezone_set() only manages timezones relative to the server clock for the PHP script, not database connections. Did you try using Yii::app()->setTimeZone()? The manual says it’s just another wrapper for date_timezone_set() so it shouldn’t do any difference with database connections really, unless it’s undocumented.

I store all times in UTC, then all times in the database are consistent with each other. You can use the MySQL function UTC_TIMESTAMP() to get this for insert and comparison. Then you convert the time for display purposes only and I make use of the DateTime object in PHP (http://uk.php.net/manual/en/class.datetime.php). So for example in the view you would have something like:

$tzUtc = new DateTimeZone( 'UTC' );

$tzUser = new DateTimeZone( $user->time_zone );

$date = new DateTime( $model->start_date, $tzUtc );

$date->setTimeZone( $tzUser );

echo $date->format( 'd/M/Y H:i' );

Hey Sayten, that’s an elegant way to do it. Just a question out of laziness: Is it possible to use integers instead of timezone labels, such as: $myTz = new DateTimeZone(’+7’) or similar?

Hi, Never done myself but according to this page, I think that you can use: ‘Etc/GMT+7’. You may have problems with DTS, since I wouldn’t assume that ‘Etc/GMT+7’ may include correction for DTS (but for example ‘America/Denver’ may have it)

Hope this helps

I’ve not tried but if rurbinac is right and you want to avoid DST issues you could use ‘UTC+7’.

Thanks guys! I tried using ‘Etc/GMT+7’ and it works, however it seems to require that the server is configured for GMT time as well. At least on my server, whose hardware clock is set to GMT and server o/s is configured for local time zone (WIT), it simply added another 7 hours to local time. Or could it be a matter of configuring PHP.INI? This behavior seems a bit strange…

my application is displaying time in EST how can i convert it to PST just by a single change I don’t want to change all files I just want to add timezone in main.php,actually i did that but still there is no difference in time