简体   繁体   中英

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!

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.

It is impossible by definition.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM