<?php function time_until($event, $day, $month, $year = "unset", $min = 0, $hour = 0, $sec = 0) {
/* prints the time until a given date. Can calculate to any time of day,
* but defaults to midnight
* by Rob Fisher 8/3/2000
*/
/* If we've not been given a year, assume it's this year */
if ($year == "unset")
$year = date("Y");
$left = mktime($sec, $min, $hour, $month, $day, $year) - mktime();
// what if the event has already happened? Let's assume it's an annual
// event (I wrote this function to calculate the time until Christmas
// after all) and work out how long until it happens next year. This may
// or may not be suitable behaviour for your application
if (0 > $left)
$left = mktime($sec, $min, $hour, $month, $day, $year + 1) - mktime();
// if we've hit the event bang on the nose, just say so
if ($left == 0)
return "it's $event";
$time = array(
"year" => 31449600,
"week" => 604800,
"day" => 86400,
"hour" => 3600,
"minute" => 60,
"second" => 1);
$ret_str = "There ";
while(list($span, $scale) = each($time)) {
$chunk = floor($left / $scale);
$left = $left % $scale;
if ($chunk != 0):
$p1 = ($chunk == 1) ? "is" : "are";
if ($chunk > 1)
$span = "${span}s";
$sep = ($left == 0) ? " and " : ", ";
$ret_str .= ((!ereg("[0-9]", "$ret_str")) ? $p1 : $sep) .
" $chunk $span";
endif;
}
return $ret_str . " until ${event}.";
}
/* As an example, how long until Christmas? */
echo time_until("Christmas", 25, 12); ?>
|
|