|
JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
|
Thread Tools | Rate Thread | Display Modes |
#1
|
|||
|
|||
Count down script time inaccuracy
I have a simple countdown script that takes an parameter for the expiration date and creates a count down from now until the expiration. Everything is handled using unix timestamps.
here is the javascript Code:
function countdown(exp_time){ var now = Math.round(new Date().getTime() / 1000); //alert(now); var time_til = exp_time - now; if(time_til > 0){ days = Math.floor(time_til/(60*60*24)); time_til %= (60*60*24); hours = Math.floor(time_til/(60*60)); time_til %= (60*60); minutes = Math.floor(time_til/60); time_til %= 60; seconds = time_til; document.getElementById('count_update').innerHTML = days +':'+hours+':'+minutes+':'+seconds; setTimeout('countdown('+exp_time+');', 1000); } else{ document.getElementById('count_update').innerHTML = 'No Time Remaining!'; } } Code:
$expired = mktime(0,0,0,MM,DD,YYY); <div id='count_update'></div> <script type='text/javascript'>countdown(<?php echo $expired]; ?>)</script> The script seems to work fine except for one thing. The time shown on the countdown does not expire at 12am on the servers clock. Its anywhere from 1 to 4 hours off depending on the server it is running on. Am I over looking something that I should be adding in the js to make the clock accurrate? Thanks alot for any help -Kyle |
#2
|
|||
|
|||
After playing with this for a while it appears GMT has something to do with the offset, hence it's always exactly 1 hour out.
Try changing: HTML Code:
var now = Math.round(new Date().getTime() / 1000); HTML Code:
var now = new Date(); var offset = now.getTimezoneOffset(); var now = Math.round(new Date().getTime()/1000)-(offset*60); Hope that helps somewhat |
#3
|
|||
|
|||
Please note, your code will also currently generate a PHP parse error due to the following bit of code:
PHP Code:
|
#4
|
|||
|
|||
Quote:
Thanks a lot. I believe you additions have made it more accurate. The parse error was just in the sample code there. I am actually pulling the expiration date from a stored value in the database. I just forgot to remove the ] when editing the code to post here as an example. Thanks again |
#5
|
|||
|
|||
Still not sure if it is exactly right. Should I be accounting for time zone offsets when i store the expiration date in the database and not just use mktime() alone?
|
#6
|
|||
|
|||
You should store the date/time generated by PHP (ie. the server time) rather than what is generated by Javascript (ie. the time of the users computer).
|
Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|