views:

851

answers:

2

I have an application which uses Zend_Date to display dates. Zend_Date instances are created using datetime data from MySQL, user input and the current date.

I would like for my users to be able to specify their timezone and for all dates to be displayed in their local time.

At the moment my code works like this:

$date = '2009-01-01 10:30:00';

$date = new Zend_Date($date, Zend_Date::ISO_8601);

echo $date->get(Zend_Date::TIME_MEDIUM); //10:30:00

$date->setTimezone('Australia/ACT');

echo $date->get(Zend_Date::TIME_MEDIUM); //21:30:00

This works, but requires a setTimezone call on every date. Is there an easier way to manage timezones?

I've also been looking at using SET time_zone with MySQL, to return adjusted data from MySQL. Then I would only need to adjust dates created within PHP scripts for timezones.

I'd love to hear the best way to handle this, if someone has experience.

Thanks

+3  A: 

I think that setting the PHP timezone should set the default for all subsequent Zend_Date instances. For example:

date_default_timezone_set('Europe/Vienna');

From the Zend_Date section in the Zend Framework Reference Guide:

In PHP, we can adjust all date and time related functions to work for a particular user by setting a default timezone according to the user's expectations. When creating Zend_Date instances, their timezone will automatically become the current default timezone!

Tim Wardle
Thanks. It seems that the best way is for me to set UTC as the default, work with UTC datetime internally, and use setTimezone to produce timezone specific local times.
David Caunt
A: 

I think you could make use of the Zend_Locale, read some docs about it, pretty sure you'd be able to make it work. On the other hand, if you use Zend_Cache and Zend_Locale/Zend_Date, that's gonna help up improve the speed by quite a bit. There are examples of usage in the zend framework docs too.

drailean