1

I have a countdown timer which transfers time from H:i:s to long version using this script:

function parseTime() {
var timeLeftStr;
var timeLeft = 0;

timeLeftStr = document.getElementById("timeleft").innerHTML;

timeLeftStr.replace(/(\d+):(\d+):(\d+)/, function () {
    for (var i = 1; i < arguments.length - 2; i++) {
        // Convert to ms
        timeLeft += arguments[i] * Math.pow(60, 3 - i) * 1000;
    }
});

countdown(new Date(timeLeft));
}

function countdown(timeLeft) {
var hours = timeLeft.getHours();
var minutes = timeLeft.getMinutes();
var seconds = timeLeft.getSeconds();

if (timeLeft.valueOf() == 0) {
    document.getElementById("timeleft").innerHTML = "0 seconds";
window.location = 'home.php?pageid=' + getURLParam("pageid");
    return false;
} else {
    document.getElementById("timeleft").innerHTML =
        (hours == 0 ? "" : hours + (hours == 1 ? " hour" : " hours")) +
        (minutes == 0 ? "" : (hours ? (seconds ? ", " : " and ") : " ") + minutes + (minutes == 1 ? " minute" : " minutes")) +
        (seconds == 0 ? "" : (hours || minutes ? " and " : "") + seconds + (seconds == 1 ? " second" : " seconds"));

    setTimeout(function () { countdown(new Date(timeLeft - 1000)); }, 1000);
}
}

window.onload = parseTime;

The error is that a user of mine who lives in Australia keeps getting the wrong "hour" The original timer would say something like "23:45:05" but when the countdown timer starts it says "10 hours, 45 minutes and 5 seconds" rather than 23 hours.

Any idea why this could be happening? Thank you.

Im not that great at JS, this was created by a friend.

Worked it out in the end.

Sam Ham
  • 111
  • 2
  • 16

1 Answers1

0

well, this happens because javascript gets the current date and time from the client: so when it's 23 o'clock in your country, in another part of the world a client who make a request to the page will show a different hour (and probably day)

if you want to show the same time everywhere you should get the server date and time so you're sure it's the same for every client or you should get the local timezone and apply an offset.

You could take a look a this similar discussion: Javascript countdown using absolute timezone?

Community
  • 1
  • 1
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
  • How can I make it take the server time? – Sam Ham Apr 02 '12 at 15:23
  • How exactly would I get the script to get the server time rather than the local time? – Sam Ham Apr 02 '12 at 15:27
  • make an ajax call to a server-side resource sending back hour, minutes and seconds. if it sounds too complicated for your capabilities just use `getTimezoneOffset()` in javascript. Please have also a look to this discussion: http://stackoverflow.com/questions/8805613/javascript-countdown-using-absolute-timezone – Fabrizio Calderan Apr 02 '12 at 15:31
  • JavaScript is actually complicated for me too. I write PHP only. I will try to have a go though, thanks. – Sam Ham Apr 02 '12 at 15:33
  • How can I implement it into my current script? I actually don't have a clue how to fix it, I will end up making it worse. – Sam Ham Apr 02 '12 at 15:36