views:

333

answers:

2

Hello!

I'm writting an application using Zend_Framework (so the solution can rely on it).

How to get client's timezone?

For example, if someone in Moscow, Russia, I want to get 3*60*60 (because there is UTC+3). If he is in UK, I want zero. If he uses UTC-3:30 (Canada?), I want -3.5*60*60.

(it's not a question about a format - I'm ok with getting 'Europe/Moscow' or 'UTC-3' for St. Petersburg, Russia, it's rather about getting timezone client uses. But delta in seconds is preferred)

The only solution which comes to mind is letting javascript get the local time for me and redirect.

A: 

I think your best bet is to guess based on the Accept-Language header sent over by the client.

Myles
to retrieve the country and city I can use geoip. But timezones like 'Europe/Moscow' can be set only for specific cities, while client can be from anywhere
valya
It's true, and the Accept-Language will most likely cover a large area of timezones, but the client doesn't really send anything else over that indicates their timezone. The most accurate would be your suggestion of redirecting based on javascript, but that is too bad to have two page loads per page request...
Myles
I can store it in a session then :) It's only one additional request. I think, I gonna go this way if I don't find anything else.
valya
+3  A: 

Check out this article on how to detect the timezone by setting a Cookie through JavaScript that will hold the client's timezone. It's rather lenghty, but that is because it is quite verbose. I've implemented a solution along these lines in one of my own apps and it works quite well.

You could also send the timezone via Ajax to the server and have it do whatever you need to do it with then. Or, if you are not doing any serverside calculations with it, just apply the timezone client side where needed. Really depends on your usecase.

In addition to that, I suggest you let the visitor set his timezone himself and store that in the Cookie or a Session.

Gordon