convert time into different timezones

Hi all,

    I have an application that is hosted at timezone gmt: -5.0 and my clients are at different time zones like gmt: 2, gmt -3 etc. I want to show the time with respect to their timezone when they access the application.

    Is there any good way in yii to achieve this?

Thanks in advance!

put in your main.php this string


the format of describing timezone you could find in PHP manual

Thanks for the reply!

  I can set the timezone of my application in php.ini. if i set 'timeZone'=>"Asia/Taipei"  as a timezone in php.ini and if I call date("Y-m-d H:i:s"), it will return date with this timezone. It is fine, but I think i need to elaborate my requirements.

  I have an application hosted at time zone GMT: -5 and this is accessed by many clients in different timezones like GMT: -2 etc. They can create insert some data record in my application and it will insert date according to my server time zone, but while showing to my clients I need to convert this into the client timezone - I need to show CreatedDate based on client timezone. 

 I hope you can understand me!

Hi again!

Finally what i want is to convert server time  to some other timezone in yii framework. Is there any way in yii framework?


This is something that can be handled with PHP and/or MySQL. With PHP you can do something like the following:


echo date('Y-m-d H:i:s', strtotime('now'))."\n";


echo date('Y-m-d H:i:s', strtotime('now'))."\n";

This will change the timezone associated with the PHP time functions. Here is a list of valid timezones:

With MySQL you have both a global timezone as well as a per-session timezone setting. If your fields are stored as timestamps (this does not work for datetime) then it will automatically convert the time stored in the field to the correct time in the set timezone. See:

mysql> CREATE TABLE `time_test` (

    ->   `dt` datetime NOT NULL,

    ->   `ts` timestamp NOT NULL,

    -> );

mysql> SET time_zone = '-2:00';

mysql> INSERT INTO `time_test` SET `dt` = NOW(), `ts` = NOW();

mysql> SELECT * FROM `time_test`;

-- You will notice that the times will be the same, now change the timezone

mysql> SET time_zone = '+8:00';

mysql> SELECT * FROM `time_test`;

-- Now notice that the value of the timestamp has moved forward 10 hours, while the datetime has remained the same.

All this being said, now you have two options for how to control what values are stored, depending on which median you would prefer.

My initial thoughts are that you could implement an event handler for ‘beforeAction’ in your base controller. If the user is logged in, read their preferred timezone from their config and set it in MySQL or PHP. You could also create a new child class of application that, upon initialization does all this checking and set the related item.

Keep in mind that with both these method you need to store everything in terms of timestamps. PHP and MySQL are both smart enough to generate the same timestamp, regardless of the timezone that is used:


$ts = strtotime('now');

echo $ts.' => '.date('Y-m-d H:i:s', $ts)."\n";


$ts = strtotime('now');

echo $ts.' => '.date('Y-m-d H:i:s', $ts)."\n";



1260599676 => 2009-12-11 23:34:36

1260599676 => 2009-12-12 01:34:36

Storing items as a date string, in either case, will probably ruin the ability to easily convert between timezones.

Does that help?

Thanks for your reply killermonk!

   I got your suggestion, but I can't change my php server timezone on demand, because other pages are also accessing the server and calling date. So the date will get messed up on those pages. 

   So if I have a date in 'US/Mountain' timezone, how do I convert it into the 'US/Eastern' timezone date without changing my php server timezone using yii framework? This is my simple question. One more thing these dates are datetime fields in mysql db.

Thanks already!

date_default_timezone_set() only changes the time zone for the rest of your current PHP script. It will not be persistent.


 I will try to go with php default time zone settings.

Mike said exactly what I would have said for date_default_timezone_set().

If your fields in the MySQL database are datetime fields, then the conversion will need to happen in PHP. Just change the timezone before you call strtotime on the data returned from the database.