TimeZone list understanding

╄→尐↘猪︶ㄣ 提交于 2019-12-20 06:29:34

问题


Currently i have a list of timezone fetched from php $tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL); which contain 400+ timezone is there any way i can use these timezone list instead of that huge list.


回答1:


What we do is pick a "representative" timezone for each of the timezones we want to show. So, our drop-down looks something like this:

<select>
    <option value="America/New_York">US Eastern Time</option>
    <option value="America/Los_Angeles">US Pacific Time</option>
    <option value="Asia/Tokyo">Japan Standard Time</option>
    <option value="Australia/Sydney">Australia Eastern Time</option>
</select>

The value part is what we save into the database of the user, but they pick based on the "nicer" names they're likely to be familiar with. This also works better than a GMT offset, as it takes into account things like daylight savings automatically.

Another potential option is having the user pick a country first. If you do that, you can then pass the second optional parameter to listIdentifiers:

DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'AU')

which will limit the output to just valid timezones within that country:

[
    "Antarctica/Macquarie",
    "Australia/Adelaide",
    "Australia/Brisbane",
    "Australia/Broken_Hill",
    "Australia/Currie",
    "Australia/Darwin",
    "Australia/Eucla",
    "Australia/Hobart",
    "Australia/Lindeman",
    "Australia/Lord_Howe",
    "Australia/Melbourne",
    "Australia/Perth",
    "Australia/Sydney",
]

Some will even make it easy on you, and spit out only one timezone, which'll let you guess pretty accurately which timezone that user is likely to be using...

DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, 'FR')

[
    "Europe/Paris",
]


来源:https://stackoverflow.com/questions/54579025/timezone-list-understanding

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