简体   繁体   English

将时区偏移转换为名称

[英]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. 我使用Javascript来存储用户当前的GMT偏移时区,我想将其转换为PHP时区名称。

If I have an offset of say "300" or "-200" how can I convert it into the php timezone Name? 如果我有一个说“300”或“-200”的偏移,我怎么能把它转换成php时区名?

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. 其中$offset是以小时为单位的时间偏移量。 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. 由于PHP中存在一些已知的错误/特性,这种简化方法在某些条件下可能会失败,因此有一个带有解决方法的扩展包装器,可以在php.net站点上找到。 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. 实际上,正如@zerkms在他的回答中所指出的,时间偏移和时区名称之间没有一对一的关系,因为几个时区通常共享相同的偏移量。 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. 但无论如何,此功能非常便于通过date_default_timezone_set为用户会话设置首选时区,该时区仅接受时区标识符,但用户可能会在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) 几个时区可能具有相同的偏移量(并且还取决于一年中的时间)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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