Viewed   265 times

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.



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.

Tuesday, October 18, 2022

The fragment part of the URL is never sent to the server via GET requests (or any kind of HTTP request for that matter), the only way you can get it is if you write a Javascript snippet that parses the URL and sends the fragment back to the server via Ajax for instance.

This can't be done with PHP alone.

Saturday, August 6, 2022

The main advantage of using the Registry with a config is to avoid polluting the global namespace. Say, you want to include a third party lib and your app and the lib both define a constant DB_HOST. No good.

In addition, many of the factories in Zend Framework utilize the config object to create instances. Zend_Db is a good example of this. You just pass it $this->database and it will take what it needs to instantiate your adapter.

You can also extend the registry with custom functionality, e.g. finder methods or stuff like that. Check the pattern description by Fowler.

Friday, October 7, 2022

A very simple method to solve this question:

import time

def localTzname():
    offsetHour = time.timezone / 3600
    return 'Etc/GMT%+d' % offsetHour

Update: @MartijnPieters said 'This won't work with DST / summertime.' So how about this version?

import time

def localTzname():
    if time.daylight:
        offsetHour = time.altzone / 3600
        offsetHour = time.timezone / 3600
    return 'Etc/GMT%+d' % offsetHour
Tuesday, August 23, 2022

If you can call a web service, you might like to try For example:


<Response> <Ip></Ip> <Status>OK</Status> <CountryCode>US</CountryCode> <CountryName>United States</CountryName> <RegionCode>41</RegionCode> <RegionName>Oregon</RegionName> <City>Corvallis</City> <ZipPostalCode>97333</ZipPostalCode> <Latitude>44.4698</Latitude> <Longitude>-123.343</Longitude> <TimezoneName>America/Los_Angeles</TimezoneName> <Gmtoffset>-25200</Gmtoffset> <Isdst>1</Isdst> </Response>

A faster option is to use the free MaxMind Geolite city. If it is not good enough, you can apparently upgrade to a more accurate paid-version. I can't speak for the quality of the paid version, as I have never used it. You can download the file binary blob version of the same database, and then use the C# class to query it.

Tuesday, December 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :