It runs in Opera... Its not pretty by any means and quick and rough.
Fuller Version Here
Code:
function tween(rfcDateBegin,rfcDateEnd){
rfcDateBegin = rfcDateBegin || false;
rfcDateEnd = rfcDateEnd || false;
if( rfcDateBegin && rfcDateEnd ) return Math.floor( new Date(rfcDateEnd).getTime() - new Date(rfcDateBegin).getTime() );
return null;
}
Date.prototype.getNextDow = function(dow){
dow = dow || 0;
// finds first dow from day in date object in the month set in the date object and likewise the year.
for(dayNo=this.getDate(); (this.getDate()+9)>dayNo; dayNo++){
futureDate = new Date(this.getFullYear(), this.getMonth() , dayNo);
// we only check forward 9 days, if we don't find anything then we have a problem!
if( futureDate.getDay()==dow ) return futureDate.getTime(); // return date value
}
return false; // we return something because we have to!
}
Number.prototype.convertToReference = function(){
// reduce to seconds
tmp = Math.floor( this / 1000 );
r = [];
days = Math.floor(tmp / ( 60 * 60 * 24 )); // number of days
if( days>0){ r.push( days , " Days, " ); tmp -= ( days * 60 * 60 * 24); }
hours = Math.floor( tmp / ( 60 * 60 ));
if( hours>0){ r.push( hours , " Hrs, " ); tmp -= ( hours * 60 * 60 ); }
minutes = Math.floor( tmp / 60 );
if( minutes>0 ){ r.push( minutes, " Mins, "); tmp -= ( minutes * 60 ); }
r.push( tmp , " Secs.");
return r.join("");
}
function update(){
// get the time now.
nowObj = new Date();
now = new Date().getTime();
// get the nearest Friday from 'now
target = nowObj.getNextDow(5); // 5 == friday
remaining = tween( now , target + offset );
tg.value = remaining.convertToReference ();
}
offset = tween("1 Jan 2012 00:00:00","1 Jan 2012 16:00:00"); // ==57600000 ms, this is midnight to 4pm as milliseconds
setInterval( update , 1000);
Assumes you have an imput field and body tag set up like so...
PHP Code:
<body onload="tg=document.getElementById('remain');"><input type="text" id="remain" value="" size="50" />
No provision for zero time, that bit you need to work out... Only times between now and target time. Error checking, none!
Does this help? Can anyone improve on this using the functions already used in the example I posted?