在PHP Web应用程序中构建时区功能 [英] building timezone feature in PHP web application
问题描述
这几乎与此问题类似:-
This is almost similar question to this one: -
我阅读了该线程,并遇到了更多问题,并且不确定如何在PHP/MySQL应用程序中优雅地构建时区支持.
I read that thread, and have some more issues, and not sure of how to elegantly build timezone support in PHP/MySQL application.
我正在用PHP编写应用程序,用户可以在其中选择时区.
I am writing an application in PHP, where users can select their timezone.
在这里,所有时间戳都存储在MySQL的GMT中.当显示数据时,我将时区转换回用户时区.
Here, all timestamps are stored in GMT in MySQL. And I am converting the timezone back to users timezone, when presenting the data.
现在这些是我面临的问题:-
Now these are the problems I am facing: -
1) 我向用户展示了一个从MySQL的时区表填充的时区列表,这是一个巨大的列表,很难选择.
1) I show the user, a list of timezones populated from MySQL's timezone tables, it is a HUGE list and is difficult to chose from.
因此,我想实现类似于Windows时区列表的功能.偏移区域信息.
So, I want to implement something like windows time zone list. Offset with region info.
2) PHP和MySQL可能具有不同的列表集.
2) The PHP and MySQL may have different set of list.
3) 在PHP或MySQL中应在哪里转换时区?我有编码的组合. 有时很容易选择从MySQL转换的列,有时很容易用PHP转换. 因此,在这里,上一点变得非常重要.例如,在MySQL的time_zone表中 Asia/Kolkata和Asia/Calcutta都在那儿,但是在PHP 5.2.6的默认时区db中,仅存在Asia/Calcutta.
3) Where should I convert timezones, in PHP or in MySQL? I have combination of coding. Sometimes it is easy to select a column converted from MySQL, sometimes it is easy to convert in PHP. So, here the previous point becomes very important. For example, in MySQL's time_zone tables Asia/Kolkata, and Asia/Calcutta both were there, but in PHP 5.2.6's default timezonedb, only Asia/Calcutta is present.
推荐答案
When faced with this exact issue, I found this reference, which maps the succinct, Windows-style timezone list to a subset of the ridiculously exhaustive Unix-style timezone list.
向用户显示这些Windows样式名称的下拉列表(例如(GMT-05:00)美国东部时间(美国和加拿大)),并且他们的选择以Unix样式格式存储在db中(例如America/New_York)
Users are presented with a dropdown of these windows-style names (e.g., (GMT-05:00) Eastern Time (US & Canada)), and their selection is stored in the db in the unix-style format (e.g. America/New_York)
The work of applying the user's timezone preference is done in PHP at display time, using the DateTime class. I think I'd recommend this, so you can be confident that the dates you're manipulating in SQL/PHP are always in UTC, until they're displayed.
这篇关于在PHP Web应用程序中构建时区功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!