PHP - Countdown to date
From Global Programming Syntax
Sometimes on a website you may want to have displayed on a page the number of days untill an event. But it can also be hard work updating this number every day unless you write a PHP script to update it for you. Below is a simple script that allows you to display the number of hours, days or months untill a specified date. First place the following at the top of your PHP file.
<?
function countdown($ddday,$ddmonth,$ddyear,$ddtype)
{
$dddays=$ddday-date(j);
$ddmonths=$ddmonth-date(n);
$ddyears=$ddyear-date(Y);
if ($ddmonths<12)
{
$ddmonths=$ddmonths*30.5;
} else {
$ddmonths=$ddmonths*(365/12);
}
$ddyears=$ddyears*365;
$ddoverall=$dddays+$ddmonths+$ddyears;
if ($ddtype=='houres' || $ddtype=='hours' || $ddtype=='hour' || $ddtype=='houre')
{
$ddoverall=$ddoverall*24;
return $ddoverall;
} else
if ($ddtype=='day' || $ddtype=='days')
{
$ddoverall=round($ddoverall);
return $ddoverall;
} else
if ($ddtype=='month' || $ddtype=='months')
{
$ddoverall=$ddoverall/(365/12);
$ddoverall=round($ddoverall);
return $ddoverall;
}
}
After you have placed the above code at the top of your php file, you then will need to call the function to display how many hours/days/months until the date of choice. So below is an example of using the function.
// Basic formatting of the function
// countdown(day,month,year,return_as);
//-------------------------------------
//each line below displays same date but for hours, days & months
echo "hours=". countdown(31,12,2010,'hours') ."<br>";
echo "days=". countdown(31,12,2010,'days') ."<br>";
echo "months=". countdown(31,12,2010,'months') ."<br>";
