0

On my site I am getting the current time via ajax function that returns the servers current timestamp and then creating a date object.

$.post(flipCountdownObj.ajax_url, data, function(response) {

        var currentTime = new Date(parseInt(response) * 1000);
        alert("Server says the time is " + currentTime.toLocaleTimeString());

        // code to create my countdown here...

}

My logic then goes on to compare that date to various other dates to provide countdowns and event status.

My problem is though that everyone is getting different times.

I am in GMT. The server is in EST. For me it says the correct time but people in EST are saying that it is 4 hours out.

I don't understand why this is happening as the timestamp should be the same for everyone.

I saw a post saying about setting UTC time but not sure exactly what I am meant to do. Can anyone shed some light on what I am doing wrong?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • i've found a way a while ago where i use JS Date to figure out the browser timezone and add this difference in a cookie so i could calculate the UTC in PHP. see http://stackoverflow.com/questions/6939685/get-client-time-zone-from-browser. But you can do this with JS only. – Louis Loudog Trottier Feb 06 '17 at 16:45

1 Answers1

0

This seems to work:

    var serverOffset = -300*60000; // -5 hrs is 300 minutes

    var usersDate = new Date();
    var userOffset = usersDate.getTimezoneOffset()* 60000;

    var currentTime = new Date((parseInt(response) * 1000) + userOffset + serverOffset);
Guerrilla
  • 13,375
  • 31
  • 109
  • 210