Hi there,
I'm writing a script that closes a page off at 2pm every sunday until 6pm, and on that page there's a countdown, which counts down to 2pm sunday.
Only, it doesnt! The second Sunday arrives the countdown displays all strangely, at the moment i'm using strtotime() :rolleyes:
Any ideas what i could to to fix the following code?
$time = strtotime("2pm next Sunday") - time();
$timeleft = $time1 - 50400;
function duration($secs)
{
$vals = array('w' => (int) ($secs / 86400 / 7),
' Days' => $secs / 86400 % 7,
' Hours' => $secs / 3600 % 24,
' Minutes' => $secs / 60 % 60,
' Seconds' => $secs % 60);
$ret = array();
$added = false;
foreach ($vals as $k => $v) {
if ($v > 0 || $added) {
$added = true;
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
$pieces = explode(" ",duration($timeleft));
I can see the glaring problem with strtotime already. Ideally i'd like to get away from using it.. I don't really think it's a good idea to use text for numeric calculations! lol
So, the above code creates the exploded pieces, which are then used in this javascript code:
echo'
<script type="text/javascript">
var days = '.$pieces[0].'
var hours = '.$pieces[2].'
var minutes = '.$pieces[4].'
var seconds = '.$pieces[6].'
function setCountDown ()
{
seconds--;
if (seconds < 0){
minutes--;
seconds = 59
}
if (minutes < 0){
hours--;
minutes = 59
}
if (hours < 0){
days--;
hours = 23
}
document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
setTimeout ( "setCountDown()", 1000 );
}
</script>
';
Any help at all would be really appreciated 🙂
Thanks,
Dave