ackerchez
ackerchez

Reputation: 1744

Converting Timezone Offset into Name

I am using Javascript to store a user's current GMT offset timezone and I would like to convert it to the PHP Timezone Name.

If I have an offset of say "300" or "-200" how can I convert it into the php timezone Name?

Thanks!

Upvotes: 7

Views: 7349

Answers (2)

Stan
Stan

Reputation: 8768

Unfortunately, I came across your question just now, so the answer may seem late a bit, but nevertheless I'm posting it.

You definitely can convert time offset into timezone name. Basically this is performed by the following line of code:

$zoneName = timezone_name_from_abbr('', $offset*3600);

where $offset is a time offset in hours. This simplified method may fail under certain conditions due to some known bugs/features in PHP, so there is an extended wrapper with a workaround, which can be found on php.net site. Among other things, the wrapper supports daylight savings flag as well.

Indeed, as @zerkms noted in his answer, there is no one-to-one relation between time offset and timezone name, because several timezones do usually share the same offset. This function returns the first found time zone corresponding to given offset. Which one exactly goes the first, is not predefined.

But anyway, this function is very handy for setting preferred timezone for a user session via date_default_timezone_set, which accepts timezone identifier only, but user may be presented with time offsets in web UI. We don't care which identifier is used (behind the scenes) as long as we know that time offset is correct.

Upvotes: 7

zerkms
zerkms

Reputation: 254916

It is impossible by definition.

Several timezones may have the same offset (and it is also depends on the time of the year)

Upvotes: 3

Related Questions