This tutorial will teach you how to create a simple event timer. An event timer will allow people to see the exact amount of days until something happens, such as a Birthday or webpage grand opening.
In the code below, I made a Birthday timer, but the code can be easily modified to time something else! Just follow the comments.
<?php
<?php
function time_until($event, $day, $month, $year = "unset", $min = 0, $hour =
0, $sec = 0) {
/* The above prints the time until a given date.
This can calculate to any time of day. */
/* If you didn't put a year above ($year = "unset"), assume it's the current year */
if ($year == "unset")
$year = date("Y");
$left = mktime($sec, $min, $hour, $month, $day, $year) - mktime();
/* Lets assum that this is an annual event such as a Birthday and that
it will happen again next year. This starts the counter over again after
the event takes place. */
if (0 > $left)
$left = mktime($sec, $min, $hour, $month, $day, $year + 1) - mktime();
/* If there is no time left on the counter (the day has come)
Change the "My" if you want, but leave the $event for the event name
This is set so it will say: "My Birthday" */
if ($left == 0)
return "My $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):
/* if there is more than one day left, have are, for 1 day have is */
$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 my ${event}.";
}
/* As an example, how long until your Birthday comes? */
/* ("Event", day of month, month); */
echo time_until("Birthday", 12, 9);
?>
?>
Place this in a seperate php file and include it using something like php includes, or simply write it in your BODY tag where you want it to appear.
If you follow the comments correctly, you should come out with a wonderful way to time the days left for an event! This code can be heavily modified to do many things, so try it out! |