PDA

View Full Version : Count down timer



brndn
October 14th, 2009, 05:56 AM
Hi all just mashed up this from a few examples I found but is a handy way to count down days, hours, minutes..




<?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;
?>

simplistik
October 14th, 2009, 02:12 PM
pretty cool ... but


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


should probably be arguments of the function:



function datum($datum=true,$sign = "+",$h = "0",$dst = true) {

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;
}

}