views:

428

answers:

3

Hi,

I have a calendar build in JS that compares dates with PHP. The JS date object is set using PHP but when I compare future dates they appear to be out of sync.

PHP is set to GMT and JS is set to UTC, how do these standards differ and could this be causing the problem?

Thanks

+2  A: 

From Coordinated Universal Time on Wikipedia:

Coordinated Universal Time (UTC) is a time standard based on International Atomic Time (TAI) with leap seconds added at irregular intervals to compensate for the Earth's slowing rotation.

From Greenwich Mean Time on Wikipedia:

UTC is an atomic time scale which only approximates GMT with a tolerance of 0.9 second

Developer Art
Which, for pretty much any application should be something you can ignore. Exceptions are very rare and those exceptions are very unlikely to be written in PHP anyway.
Joey
+2  A: 

One is measured from the sun and another from an atomic clock.

For your purposes, they are the same.

Ben James
A: 

Are you accounting for connection times and lag? If you're using AJAX, you need to make sure you use a time offset for the length of time it takes to connect and download the response:

var XHR = new XMLHttpRequest();
var Now = new Date().getTime();
XHR.open("GET", "/datetime.php", true);
XHR.onreadystatechange = function()
{
    if (XHR.status == 200 && XHR.readyState == 4)
    {
        // How long did it take to get here?
        var TimeOffset = new Date().getTime() - Now;

        // ... your code to get the date from the response goes here

        // Now add the TimeOffset variable to the date
        var SyncTime = new Date().setTime(PHPTime + TimeOffset);
    }
}
XHR.send();

Of course, that code's just to give you a general idea and will probably need adjusting to account for timezone offsets etc.

Andy E