Convert Timezone Offset to Name

I am using Javascript to store the current GMT time zone and I would like to convert it to the PHP time zone name.

If I have an offset of "300" or "-200", how can I convert it to php timezone name?

Thanks!

+6
source share
3 answers

Unfortunately, I just came across your question, so the answer may seem a bit late, but nonetheless, I am posting it.

You can definitely convert the time offset to the name of the time zone. This is mainly done by the following line of code:

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

where $offset is the time offset in hours. This simplified method may fail under certain conditions due to some known errors / functions in PHP, therefore there is an extended shell with a workaround that can be found on the php.net website. Among other things, the shell also supports daylight saving time flag.

Indeed, as @zerkms noted in his answer, there is no one-to-one relationship between the time offset and the time zone name, because multiple time zones usually have the same offset. This function returns the first time zone found that matches the given offset. Which one comes first is not predetermined.

But in any case, this function is very convenient for setting the preferred time zone for the user session using date_default_timezone_set , which accepts only the time zone identifier, but the user can be presented with a time offset in the web user interface. We don’t care which identifier is being used (behind the scenes) if we know that the time offset is correct.

+7
source

This is not possible by definition.

Multiple time zones can have the same offset (and this also depends on the time of year)

+2
source

Here is an array with a default time zone value for each offset.

  [ -11 => 'Pacific/Pago_Pago', -10 => 'Pacific/Tahiti', -9 => 'Pacific/Marquesas', -8 => 'Pacific/Pitcairn', -7 => 'America/Whitehorse', -6 => 'Pacific/Galapagos', -5 => 'Pacific/Easter', -4 => 'America/Tortola', -3 => 'Atlantic/Stanley', -2 => 'Atlantic/South_Georgia', -1 => 'Atlantic/Cape_Verde', 0 => 'UTC', 1 => 'Europe/London', 2 => 'Europe/Zurich', 3 => 'Indian/Mayotte', 4 => 'Indian/Reunion', 5 => 'Indian/Maldives', 6 => 'Indian/Cocos', 7 => 'Indian/Christmas', 8 => 'Australia/Perth', 9 => 'Pacific/Palau', 10 => 'Pacific/Saipan', 11 => 'Pacific/Pohnpei', 12 => 'Pacific/Wallis', 13 => 'Pacific/Tongatapu', 14 => 'Pacific/Kiritimati' ] 
0
source

Source: https://habr.com/ru/post/1400101/


All Articles