Javascript Countdown Timer

I wrote a little countdown timer for my wedding while waiting for stuff to compile today. It uses PHP to get itself initialized, and should be accurate as long as the server has the correct date set. Below is the basic guts of the script. You would call refreshClock() onload.

<?php
  $now = time();
  $wedding = strtotime('August 23, 2008 4:00 pm');
  print 'end = '.$wedding.";\n// Used to fix lag, if any.\n";
  print 'now_p = '.$now.";\n";
?>
now_j = new Date();
diff = Math.ceil(now_j.valueOf()/1000) - now_p;
function refreshClock () {
  now_r = new Date();
  seconds = end - Math.ceil(now_r.valueOf()/1000) + diff;
  document.getElementById('days').innerHTML = Math.floor(seconds/60/60/24);
  document.getElementById('hours').innerHTML = Math.floor((seconds/60/60)%24);
  document.getElementById('minutes').innerHTML = Math.floor((seconds/60)%60);
  document.getElementById('seconds').innerHTML = seconds%60;
  setTimeout("refreshClock()",1000);
}

Posted May 20th, 2008 - Permalink
Categories: JavaScript - PHP - Programming - Snippets
No Comments »
 
More Posts
Newer »