How to automatically detect user's timezone?

Solution 1:

Use javascript solution

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

Demo should show your timezone in select box.

http://onlineaspect.com/examples/timezone/index.html (Dead link)

Solution 2:

You can't get a user's timezone on the server side, and you can only make a guess at it on the client side.

If you rely on getting the user's IP address then you could geolocate that and deduce a time.

The way this is usually done is by asking the user (when they register, for instance) what timezone they are in and then use this in your time calculations.

Solution 3:

For geolocation you can use http://ipinfodb.com/, work great with an API!

function GeoLocation($precision='city',$ip,$api) {

    if($precision == "country") {
        $file = "ip_query_country.php";
    }
    else {
        $file = "ip_query.php";
    }

    $params = @file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=".$api."&ip=".$ip."&timezone=true");
    $fields = @new SimpleXMLElement($params);
    foreach($fields as $field => $val) {
        $result[(string)$field] = (string)$val;
    }
    return $result;
}

Solution 4:

With a small "cheating" (using client side scripting) and posting the result to your server, you can access your client's timezone.

For example you can use this javascript library:

https://bitbucket.org/pellepim/jstimezonedetect

var tz = jstz.determine(); // Determines the time zone of the browser client
tz.name(); // Returns the name of the time zone eg "Europe/Berlin"

Demo: http://pellepim.bitbucket.org/jstz/