What is the best way of getting the timezone from the city, country that user selects?

后端 未结 2 1082
再見小時候
再見小時候 2021-01-15 16:41

I looked into javascript, and thought about using the client\'s clock, but it doesn\'t seem very reliable since the client\'s clock is out of my control.

I looked ar

相关标签:
2条回答
  • 2021-01-15 17:08

    I think this JavaScript tool jsTimezoneDetect is what you need. It is very easy to use and it can detect a pretty big range of timezones. Besides, if you want to detect the timezone from the country and city that the user selected, I think you would need to store the whole Olson timezone database and make Ajax call to get what you need.

    0 讨论(0)
  • 2021-01-15 17:10

    If you're limiting the list of cities to the cities supported by PHP it's pretty easy, you can then use the DateTimeZone class.

    If you're allowing the user to choose any old city and want to work out the timezone from that then I'm pretty sure there isn't a maintained mapping file.

    You could use a webservice to look up the latitude and longitude of a city and then plug that latitude &longitude into Ask Geo or a similar webservice. You'd need to ask the country too there's a Cairo in the USA and in Egypt.

    0 讨论(0)
提交回复
热议问题