PHP Echo Timezone based on GMT offset

筅森魡賤 提交于 2019-12-13 02:14:21

问题


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

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

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?

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


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/1923569/php-echo-timezone-based-on-gmt-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!