AlexBrand
AlexBrand

Reputation: 12419

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

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 around for web services but wasn't lucky.

Basically what I want is to display a specific time (e.g. 9:00:00 GMT +4) in the user's timezone, based on location provided (city, country).

Thanks

Upvotes: 2

Views: 3807

Answers (2)

Mr.J4mes
Mr.J4mes

Reputation: 9266

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.

Upvotes: 1

dsas
dsas

Reputation: 1670

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.

Upvotes: 1

Related Questions