17

Facebook, myspace, MSN, craigslist; all detect timezone and show times and dates correctly without ever submitting a form or reloading a page.

How do they do it? I have a pretty large site and have people publishing articles which need to display correct times (uploaded 3pm or 6pm depending on timezone).

Preloaders? Javascript? Loading the page and reloading it? My site currently has the person enter the site and upon first entry it reloads the page with updated information of their timezone through javascript. This is however a pretty annoying thing and am looking for another alternative. I know that upon loading I can send out an ajax request and then have it update everything throughout the site with the correct timezone for the user BUT if I have to click back, or reload it asks if I'd like to send the information again which is also bad. So how do they do it?

I was playing around with Facebook, changing my location on my local machine and it seemed to update the timezone every 5 minutes or so.

I am php, zend-framework based.

Darius
  • 1,613
  • 5
  • 29
  • 58

6 Answers6

16

there is no "timezone" request header, or something like that, so, you can't with php

but with javascript you can get the timezone offset of the client, then you could make a xmlhttp request to send informations to php

see this: http://www.pageloom.com/automatic-timezone-detection-with-javascript

pretty easy!

hope this helps

  • 1
    @AdamSack major sites detects timezones by registered users' info, they have their own geoip database. in plus darius said that changing the time on its computer, the change is detected, so they definitely use javascript. –  Oct 08 '11 at 17:04
  • I agree with @AdamSack still +1 for the Javascript ;) – Rakesh Sankar Oct 10 '11 at 04:45
  • 1
    Timezone offset is not constant, it changes with DST, thus it can be ambiguous what timezone a client uses. A combination of IP, js timezone offset and js locale should give better results (you need a database which combines all these somehow). – oxygen Jan 09 '13 at 09:43
6

You could find user country using IP address geocoding and then have some array with timezones for each country. I think there are no other ways. You can fetch the user country like this:

<?php
$file = file_get_contents("http://api.hostip.info/get_html.php?ip=".$_SERVER['REMOTE_ADDR']."");
?>

Then just use explode for the $file.

Look also these Stackoverflow codes:
How can I determine a web user's time zone?
Any Reliable API available to determine User's city and country from IP address

Community
  • 1
  • 1
Olli
  • 752
  • 6
  • 20
3

I believe they use IP addressing and Geo-location. That's all there is to it.

Mob
  • 10,958
  • 6
  • 41
  • 58
2

First off, you should work with UTC across all levels and if you don't know a time zone mark the time as such. Then on local client you can adjust the time with the timezoneOffset

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Adrian World
  • 3,118
  • 2
  • 16
  • 25
1

Try this php code this gives better precision

  <?php  $ip = $_SERVER['REMOTE_ADDR'];
    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
    $json = json_decode($json,true);
    $timezone = $json[localTimeZone];?>
pckabeer
  • 686
  • 6
  • 27
  • Why a down vote peeps? I see two drawbacks, using IP can give the wrong timezone if provider's servers are in another timezone, and using an exterior site is no good since we don't want to be dependent. Thanks for the input though – Darius Aug 17 '12 at 05:25
  • I didn't downvote but I think the two reasons you listed are fairly compelling reasons not to use it – sricks Dec 15 '15 at 23:45
1

jsTimezoneDetect provides a great library for automatic timezone detection via javascript.

Jordan Magnuson
  • 864
  • 3
  • 10
  • 21