Suite101

How to Create a Christmas Count Down in PHP

Calculating the Time between Dates Using PHP

© Mark Alexander Bain

Dec 2, 2008
PHP Programming With Dates, Mark Alexander Bain
Calculating the time between dates in PHP is easy but can make a nice addition to a web site - especially when the information is only displayed at the right time of year

As Christmas (or any other important date) approaches it's easy to add a count down to a web page showing the number of days left - and if that web page is PHP based then the task becomes even easier - requiring just one simple function which, once added to the web site, will show the Christmas count down automatically every December.

The PHP code must, therefore, do the following:

  • only run during December
  • only run before Christmas day
  • and, of course, show the number of days remaining between the current date Christmas Day

December Only PHP Code

All of the Christmas count down code should be encapsulated in a function; and since this is a Christmas count down, it would seem logical to have the code run only in December - but there's other criteria: that it stops running on Christmas Day and doesn't run again until next December 1st; and there's two ways to do this.

The first way to ensure that the code only runs during December and before Christmas day is to place all of the code within an if statement:

function christmas_count_down() {
if ((date('m') == 12) && (date('d') 25)) {
//Count down functionality
}
}

The second method is to exit from the function if the date criteria is not fulfilled

function christmas_count_down() {
if (!((date('m') == 12) && (date('d') 25))) {
exit(0);
}
//Count down functionality
}

Whichever method is used the next step for the programmer is to calculate the number of days remaining until Christmas Day.

PHP Code for Calculating the Days until Christmas

In order to work out the number of days between the current date and Christmas Day the PHP function needs to know what those dates are ; obviously the date for Christmas Day is simple enough:

$christmas_day = 25;
$december = 12;

The year can be obtained from the PHP method date:

$year = date('Y');

As can the current day of the month:

$tday = date('d');

It's now just a matter of converting the dates into seconds (by using the PHP mktime method) and then subtracting one from the other:

$seconds =
(mktime (0,0,0,$december ,$christmas_day,$year)
- mktime (0,0,0,$december ,$tday,$year));

The number of seconds can now be converted into the number of days:

$days = (int)($seconds/24/60/60);

Finally a formatted output can be displayed for the web page user:

if ($days > 1) {
echo "There are $days days until Christmas.";
} else {
echo "There is $days day until Christmas.";
}

Running the Christmas Count Down Function

At the moment nothing different will be displayed on the web site - for that to happen the function must be called from the PHP script:

christmas_count_down();

Server Time not Local time

It is worth noting that the dates used will be based on the server time and not the user's local time - even the PHP localtime method actually uses the server time; Javascript code is required for current time (such as the code discussed in How to Count down to Christmas in a Web Site).

Summary

With PHP it's easy to calculate the number of days between any two dates (such as the current date and Christmas Day) - to do that the programmer needs to:

  • define the target date (such as Christmas Day)
  • calculate the current day (by using the PHP date method)
  • convert both dates to seconds (by using the PHP mktime method) and subtract one from the other
  • convert the seconds to days and display the result to the web page user

And, of course, all of this should only happen in December and before Christmas Day.


The copyright of the article How to Create a Christmas Count Down in PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Christmas Count Down in PHP in print or online must be granted by the author in writing.


PHP Programming With Dates, Mark Alexander Bain
A Simple Web Page with a PHP Count Down, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo