Navigation
// Countdown to a future date
// ****************************************************************************
// configure the target date here
$base_day = 30; // no leading "0"
$base_mon = 4; // no leading "0"
$base_yr = 2012; // use 4 digit years!
// get the current date (today) -- change this if you need a fixed date
$current_day = date ("j");
$current_mon = date ("n");
$current_yr = date ("Y");
// and now .... calculate the difference! :-)
// overflow is always caused by max days of $current_mon
// so we need to know how many days $current_mon has
$current_mon_max = date ("t",mktime (0,0,0,$current_mon,$current_day,$current_yr));
// days left till the end of this month
$current_day_diff = $current_mon_max - $current_day;
// months left till end of this year
// substract one to handle overflow correctly
$current_mon_diff = 12 - $current_mon - 1;
// start on jan 1st of next year
$start_day = 1;
$start_mon = 1;
$start_yr = $current_yr + 1;
// difference to that 1st of jan
$day_diff = ($base_day - $start_day) + 1; // add today
$mon_diff = ($base_mon - $start_mon) + 1; // add current month
$yr_diff = ($base_yr - $start_yr);
// and add the rest of $current_yr
$day_diff = $day_diff + $current_day_diff;
$mon_diff = $mon_diff + $current_mon_diff;
// handle overflow of days
if ($day_diff >= $current_mon_max)
{
$day_diff = $day_diff - $current_mon_max;
$mon_diff = $mon_diff + 1;
}
// handle overflow of years
if ($mon_diff >= 12)
{
$mon_diff = $mon_diff - 12;
$yr_diff = $yr_diff + 1;
}
// the results are here:
// $yr_diff --> the years between the two dates
// $mon_diff --> the month between the two dates
// $day_diff --> the days between the two dates
// ****************************************************************************
// simple output of the results
print "Here comes Washington...";
print "
"; // this is just to make it look nicer $years = "years"; $months = "months"; $days = "days"; if ($yr_diff == "1") $years = "year"; if ($mon_diff == "1") $months = "month"; if ($day_diff == "1") $days = "day"; // here we go print ""; print $yr_diff." ".$years.", "; print $mon_diff." $months and "; print $day_diff." ".$days; print ""; ?>
"; // this is just to make it look nicer $years = "years"; $months = "months"; $days = "days"; if ($yr_diff == "1") $years = "year"; if ($mon_diff == "1") $months = "month"; if ($day_diff == "1") $days = "day"; // here we go print ""; print $yr_diff." ".$years.", "; print $mon_diff." $months and "; print $day_diff." ".$days; print ""; ?>