views:

250

answers:

1

I want to include a JavaScript world time clock like http://www.dynamicdrive.com/dynamicindex6/localtime.htm to my homepage but I don't want to change it every time when the DST changes for a time zone. Is it possible to sync it automatically with an official time server so that I can display the current time for different places live and without any changes after DST?

+1  A: 

Use the standard date function along with date_default_timezone_set.

$timezones = array("Australia/Brisbane", "America/New York", "Europe/London");

foreach ($timezones as $tz) {
    date_default_timezone_set($tz);
    echo "The time in $tz is: " . date('r');
}

It handles all the daylight savings and everything for you.

You can find the list of supported timezones here: http://www.php.net/manual/en/timezones.php


Edit: Javascript implementation:

  1. Use the above method to get PHP to output all the different timezones you're interested in, but use date('Z') to get the seconds offset from UTC. Output it using JSON or something so Javascript can use it:

    var offsets = {
        'Brisbane' : 36000,
        'Sydney'   : 42000,
        'London'   : 0
    };
    
  2. Loop through them all, adding and subtracting the offsets as necessary.

    for (var city in offsets) {
        var d = new Date();
        d.setTime(d.getTime() + offsets[city] * 1000);
        alert('The time in ' + city + ' is ' + d.toUTCString());
    }
    
  3. Put the above into a function which is called every second, using setInterval

A caveat though: it won't be guaranteed accurate, since in theory, a location's timezone could change while someone has the window open (they could be on the border of DST). It's not very likely, and a page refresh would show the right times, so it's probably not a big deal.

nickf
You forgot to mention you are using PHP when he/she wants JavaScript. I would recommend loading all initial data into some form fields and use JavaScript to update their times by polling with the setInterval function.
Jabes88
Yeah, I am already using code like that but I want to have a live updating clock with JavaScript and use the PHP code only for <noscript> users.
Peleke
@Peleke check the edit now.
nickf
Thanks but I cannot use it because I don't know how to send the date('Z') in the JavaScript offsets array dynamically and instead of the alert I want to display it like normal text which updates every second and is displayed like date('H:i (d.m.Y)') in PHP (see my link for an example). How can I do that?
Peleke
well - perhaps that's an issue for another question. The `alert` and formatting was just a proof-of-concept thing. Look at the PHP function `json_encode` and the javascript date functions here: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Date
nickf
I appreciate your effort but I have already tried to figure out how I can get it to work and I failed. I am "only" looking for an automatic DST feature for the script mentioned in my first post (http://www.dynamicdrive.com/dynamicindex6/localtime.htm).
Peleke
I fail to see what you're missing here: the above code (paired with the correct output method, which is outside the scope of this question IMO), should work. At the very least, it will get you going in the right direction. Stack Overflow isn't about getting other people to write your whole application for you, you know.
nickf