Well, first things first: Your loop will never run if you keep setting a timeout at the beginning 😉 Move the setTimeout to the end of the function so that you do the calculations first 😉
The second thing I notice is that you're not actually getting a real date. If you look at the source, you try and make a date of the difference of two dates. It just won't work.
If you look at my code, it's tested and working. You can set it to any date you want. If it's prior to 1 Jan 1970, then you're out of luck. But any date after that, and it should automatically tell you if it's expired, or count-down to it 😉
Hope it helps you out some:
<?php
$expiredate = strtotime('December 30, 2006 1:35:00 AM');
$date = array(
'y' => date('Y', $expiredate),
'm' => date('m', $expiredate)-1, // 0 - 11 in Javascript ;)
'd' => date('d', $expiredate),
'h' => date('H', $expiredate),
'i' => date('i', $expiredate),
's' => date('s', $expiredate),
);
echo'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>Countdown to Date</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript" type="text/javascript">
// Some globals ;)
var s = 1000; // seconds
var m = s*60; // minutes
var h = m*60; // hours
var d = h*24; // days
var sourcedate = new Date(' . implode(',', $date) . ');
var timeLeft = 0; // Used in calculations
var expired = false;
function checkTime(i) {
if(i<10)
{
i = "0"+i;
}
return i;
}
function countDown() {
var now = new Date();
expired = (now.getTime()>sourcedate.getTime()) ? true : false;
if(!expired)
{
timeLeft = sourcedate.getTime() - now.getTime();
//var dd = Math.floor(timeLeft/d);
//timeLeft = timeLeft-(d*dd);
var hh = Math.floor(timeLeft/h);
timeLeft = timeLeft-(hh*h);
var mm = Math.floor(timeLeft/m);
timeLeft = timeLeft-(mm*m);
var ss = Math.floor(timeLeft/s);
document.getElementById("clock").innerHTML = checkTime(hh) + ":" + checkTime(mm) + ":" + checkTime(ss);
setTimeout("countDown();", 1000);
}
if(expired)
{
document.getElementById("clock").innerHTML = "Sorry, the offer has expired.";
}
}
window.onload = function () {
countDown();
}
</script>
</head>
<body>';
echo '<strong>Offer Expires:</strong> ' . date("F d, Y @ h:i", $expiredate) . '<br />';
echo '
<strong>Time Remaining:</strong><br />
<div id="clock">--:--:--</div>
</body>
</html>';
?>