Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Countdown Back to a Date and Time

Code Wizard, 12-30-2006

Fun script I use for random countdowns, i.e. Countdown to Christmas, Spring Break, Summer, whatever. Also has a "time since" count after the countdown ends. Fun for an exact age calculation and whatnot. Really easy to use, see numerous comments.

<?

/*
Countdown Script - Prints out "X days, X hours, X minutes, X seconds until/have passed since $event
Josh Bowen - email comments/suggestions/questions to phpcomments@joshbowen.com
Open to changes/improvements, just keep my name somewhere in here, or atleast email me and show
you've found a use for it.

countdown("event string", "MM-DD-YYYY@HH:MM:SSampm");

EXAMPLE USE:
PRINT countdown("Jon's B-day","11-1-2000@11:57:20pm");
*/

function countdown($event, $datestring) {
//wordit() function used for smooth, easy printing
//and to account for the ugly "1 days/1 hours/1 minutes/1 seconds" problem
//same thing for 0 days, etc.
function wordit($number, $string)
{
if ($number == 0) {
return ""; }
elseif ($number == 1) {
return $number." ".$string; }
else {
return $number." ".$string."s"; }
}

ereg ("([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})@([0-9]{1,2}):([0-9]{2}):([0-9]{2})([am|pm|AM|PM]{2})", $datestring, $regs);

$month = $regs[1];
$day = $regs[2];
$year = $regs[3];
$hr = $regs[4];
$min = $regs[5];
$sec = $regs[6];
$ampm = $regs[7];

if ($ampm == "pm") { $hr += 12; }
if ($ampm == "am" && $hr == 12) { $hr = 0; }

$now = time();

//mktime function takes (hours, minutes, seconds, month, day, year)
//future is the time where counting down to.
//This example is Xmas - 12/25/2000 @ 12:00am
$future = mktime($hr, $min, $sec, $month, $day, $year);

if ($future < $now) { //it's already passed, oh no!
$timeleft = $now - $future; //Theoretically, future = past
$count = "PAST"; } //Becomes a "X have passed" at end.
elseif ($future > $now) { //We've still got time.
$timeleft = $future - $now;
$count = "COUNTDOWN"; }
else { //($future == $now) Hey, It could happen
$timeleft = 0;
$count = "NOW"; }

//$timeleft is used for all our day, hour, minute, second schtuff

$days = floor($timeleft/86400); //86400 seconds = 1 day
$timeleft = ($timeleft%86400);
$hours = floor($timeleft/3600); //3600 seconds = 1 hour
$timeleft = ($timeleft%3600);
$minutes = floor($timeleft/60); //60 seconds = 1 minute
$timeleft = ($timeleft%60);
$seconds = $timeleft;

if ($count == "PAST") { PRINT "Its been "; }

//Put it all together and take care of the 'and' at the end
//This is really ugly, I think it works for all possible wording. No promises. ;-)
if (wordit($days, "day") && wordit($hours, "hour"))
{ PRINT wordit($days, "day").", "; }
elseif (wordit($days, "day") && !wordit($hours, "hour"))
{ PRINT wordit($days, "day").", "; }
else { PRINT wordit($days, "day"); }

if (wordit($hours, "hour") && wordit($minutes, "minute") && !wordit($seconds, "second"))
{ PRINT wordit($hours, "hour"); }
elseif (wordit($hours, "hour") && wordit($minutes, "minute"))
{ PRINT wordit($hours, "hour").", "; }
else { PRINT wordit($hours, "hour"); }

if (wordit($minutes, "minute") && wordit($seconds, "second"))
{ PRINT wordit($minutes, "minute"); }
elseif (!wordit($minutes, "minute") && wordit($seconds, "second"))
{ PRINT ""; }
elseif (wordit($minutes, "minute") && !wordit($seconds, "second"))
{ PRINT " and ".wordit($minutes, "minute"); }

if (wordit($seconds, "second") && wordit($minutes, "minute"))
{ PRINT " and ".wordit($seconds, "second"); }
else { PRINT wordit($seconds, "second"); }

if ($count == "COUNTDOWN") { PRINT " until ".$event; }
elseif ($count == "PAST") { PRINT " since ".$event; }
else { PRINT $event." is now!"; } //consider all possibilities

} //End
?>
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions