views:

173

answers:

3

I'm using Zend_Date to manage dates/times in a scheduling app.

The only way I'm able to determine the user's Timezone is via Javascript which only gives a Timezone Offset. Zend_Date only seems to take Timezones in the 'America/New_York' format.

Is there a way to get the users timezone in this format or set the Timezone Offset with Zend_Date?

Thanks!

A: 

You can use javascript to set a cookie to the users timezone offset.

Then in php you can have an array indexed by offset

$timezones = array (

    '-1' => 'adfas/adsfafsd',
    '-2' => 'adsfasdf/asdfasdf'
    ...
);

$date->setTimezone( $timezones[$_COOKIE['tz_offset']] );
Galen
+2  A: 

Nicky,

You don't really need Zend_Date for this as the PHP intrinsic DateTime and DateTimeZone objects work well for this; however, I can point you in the right direction if you really need to use Zend_Date.

See the following examples

  1. $date = new Zend_Date(1234567890, false, $locale);
  2. $date->toString... (see: http://framework.zend.com/manual/en/zend.date.constants.html for more details)
  3. Use the following Constants for ISO 8601: (Z = Difference of time zone [+0100])

This should get you to where you need to be. Please post code samples if you get stuck.

wilmoore
BTW, better practice is to allow the user to select their timezone from a list which you store. If you want to make it easier for the user, you could present a list of guesses first.The following could help with this:The static methos "DateTimeZone::listAbbreviations()" will return a list of timezone abbreviations which also includes the offset (offset from UTC/GMT). Use your result from Javascript to search this array for a lsit of possible timezones. There will likely be dupes so present the dupes to the user and let them choose.
wilmoore
A: 

You could use the timezone_name_from_abbr function:

$javascript_offset = '-1';
$zone = timezone_name_from_abbr('', $javascript_offset * 3600, 0);

You will also need to consider DST. Have a look at this example for more info.

cbuckley