# Count down timer

**URL:** <https://forum.kirupa.com/t/count-down-timer/298144>\
**Category:** programming\
**Created:** [October 14, 2009, 9:56am UTC](https://forum.kirupa.com/t/count-down-timer/298144 "2009-10-14T09:56:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [October 14, 2009, 9:56am UTC](https://forum.kirupa.com/t/count-down-timer/298144/1 "2009-10-14T09:56:57Z")

</div>

Hi all just mashed up this from a few examples I found but is a handy way to count down days, hours, minutes…

```php
<?php
$DATE = datum();

function datum($datum=true) {
$sign = "+"; // Whichever direction from GMT to your timezone. + or -
$h = "0"; // offset for time (hours)
$dst = true; // true - use dst ; false - don't

if ($dst==true) {
    $daylight_saving = date('I');
    if ($daylight_saving){
        if ($sign == "-"){ $h=$h-1; }
        else { $h=$h+1; }
    }
}
$hm = $h * 60;
$ms = $hm * 60;
if ($sign == "-"){ $timestamp = time()-($ms); }
else { $timestamp = time()+($ms); }
//$gmdate = gmdate("M d Y H:i:s", $timestamp);

if($datum==true) {
//return $gmdate;

return $timestamp;
}
else {
return $timestamp;
}

}

function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
   
    $periods = array("second", "minute", "hour", "day");
    $lengths = array("60","60","24","7");
   
    $now = strtotime(gmdate("M d Y H:i:s", datum()));
    
    print "<p>NOW:".gmdate("M d Y H:i:s", datum())."</p><p> $now</p>";

    $unix_date = strtotime($date);
    
    
       print "<p>THEN:".date("M d Y H:i:s", $unix_date)."</p><p> $unix_date</p>";
       // check validity of date
    if(empty($unix_date)) {   
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {   
        $difference = $now - $unix_date;
        $tense = "ago";
       
    } else {
        $difference = $unix_date - $now;
        
        echo "<P>DIFFERENCE IS: $difference</P>";
        
        $tense = "from now";
    }
   
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
        
            echo "<P>DIFFERENCE: $j $difference</P>";
    }
   
    $difference = round($difference);
   
    if($difference != 1) {
        $periods[$j].= "s";
    }
   
    return "$difference $periods[$j] {$tense}";
}

$date = "2009-10-19 11:00";
$result = nicetime($date);

echo $result;
?>

```
