基于GMT偏移量的PHP Echo时区 [英] PHP Echo Timezone based on GMT offset

查看:105
本文介绍了基于GMT偏移量的PHP Echo时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将GMT时间数据存储在MySQL数据库中的应用程序,如下所示:2009-12-16 15:27:47.

I have an application that stores GMT time data in a MySQL db like this: 2009-12-16 15:27:47.

用户的时区偏移量存储在这样的另一列中:-6

The user's timezone offset is stored in another column like this: -6

以上时区为GMT-6,即CST.有什么方法可以从GMT偏移数据中回显CST(即-date('T))?

The above timezone is GMT-6, which is CST. Is there any way I can echo CST (i.e. - date('T")) from the GMT offset data?

注意:我还应该补充一点,使用以下命令设置应用程序的全局时区:date_default_timezone_set('GMT');

Note: I should also add that the application global timezone is set using: date_default_timezone_set('GMT');

推荐答案

不幸的是,每个GMT偏移量并不总是有一个时区.许多GMT偏移量都有多个时区.同样,GMT偏移会在夏令时期间(适用时)发生变化.例如,在夏令时期间,美国/亚利桑那州和美国/山地共享相同的GMT偏移量;但是,在一年的剩余时间内,US/MountainUS/Arizona不会共享相同的GMT偏移量.您可以使用 DateTimeZone::listIdentifiers()

Unfortunately, there isn't always a single timezone for each GMT offset. Many GMT offsets have multiple timezones. Also, GMT offsets change during DST (where applicable). For instance, during DST, US/Arizona and US/Mountain share the same GMT offset; however, during the rest of the year, US/Mountain and US/Arizona don't share the same GMT offset. You can either use DateTimeZone::listIdentifiers() or check out the manual for a list of supported timezones in PHP.

如果您是从头开始创建应用程序,则建议您存储时区标识符而不是GMT偏移量,但是我猜您的应用程序已在生产中.在这种情况下,没有一种可靠的方式来显示时区而不改善数据.

If you were starting an application from scratch, I would suggest storing the timezone identifier instead of the GMT offset, but I'm guessing your application is already in production. In that case, there's not really a reliable way to display the timezone without improving your data.

我建议您在表中添加一个timezone列.您可以编写一个一次性脚本,该脚本将根据GMT偏移量将timezone设置为一些默认的预定义列表.例如,如果GMT偏移为8,则将timezone设置为America/Los_Angeles.最后,将您的应用程序更改为显示/保存时区标识符,而不是GMT偏移量.这可能会给您的一部分用户带来问题,但可以让他们在需要时更改时区.

I would suggest that you add a timezone column to the table. You could write a one-time script that would set the timezone to some pre-defined list of defaults based on the GMT offset. For instance, if the GMT offset is 8, set the timezone to America/Los_Angeles. Finally, change your application to display/save timezone identifiers instead of GMT offsets. This may cause problems for a subset of your users, but it allows them to change their timezone if they need to.

这篇关于基于GMT偏移量的PHP Echo时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆