I'm looking for some help with a javascript/php code that will display a countdown from midnight server time to midnight servertime the next day. and reset its self everyday.
So far i've got this:
Code:
function ShowTimes() {
var now = new Date();
var hrs = 23-now.getHours();
if (hrs < 10) hrs = '0'+hrs;
var mins = 59-now.getMinutes();
if (mins < 10) mins = '0'+mins;
var secs = 59-now.getSeconds();
if (secs < 10) secs = '0'+secs;
document.getElementById('countdownToMidnight').innerHTML = hrs+':'+mins+':'+secs+'';
}
var _cntDown;
function StopTimes() {
clearInterval(_cntDown);
}
But obviously this does it via the users computer time so is different depending where you are.
I need it so wherever you are in the world the countdown is at the same time as everyday at midnight (servertime) an image on the website changes.
Sorry if I've done something wrong here, it's my first post.
Any help would be very much appreciated and if you require any more info just let me know
Hi
Thanks although I'm having trouble implementing this into my code.
what would i set the UTC date to and how would i use that to get the countdown correct?
Sorry for being a bit simple, im still fairly new to JS.
hmmm... but I believe that using UTC will give you the UTC time according to the user's computer.
So if I set my computer to 8:30am, January 4 1982 the code will simply translate that time to UTC.
or no?
To get the server time you have to get it from the server, which is not an easy thing with javascript, but is trivial with a server side language.
Using only javascript, you can do it via an ajax call, using the date property of the response header. See this thread (and my warning about toLocaleString)
If your clock is right your UTC time is the same as the UTC (or GMT) server time !
Then the time zone of the server is enough with something like this :
Code:
var dlt="<?php echo date('Z'); ?>";// Timezone offset in seconds. The offset west of UTC is negative, and the offset east of UTC is positive (-43200 to 43200).
It's easy to synchronize a clock with a time server. Since it's always possible to make control with UTC time (with the limitation of the propagation time) and specify the server time prevails...
Bookmarks