Creating a PHP Countdown Timer

Article by nain (944 pts ) , published Oct 16, 2009

Learn how to create a PHP countdown timer, the importance of phrasing correctly, setting a target date and the intricacies of converting from seconds to the desired periods of time.

Why PHP?

A countdown timer is a useful function that many like to incorporate into their websites; whether you're counting down to a company's product release, or for a personal even that is important to you such as a wedding or birthday. PHP is a free web-scripting software that can be embedded into HTML. The directions on how to create the timer in PHP are below. We also have directions on creating a countdown clock in HTML.

Function: ‘mktime’

Creating a countdown timer is a simple task that can add a special dimension to any page or site. Countdown timers are used on sites to let the reader know how long it is until the arrival of a special event.

The creation of a countdown timer uses the ‘mktime’ function, a simple way of phrasing a time to create what is known as a ‘timestamp’.

The mktime function is always phrased in a particular sequence – as follows:

Mktime ( hour, minute, second, month, day, year, is_dst)

The first six are self explanatory, while the final is an option and refers to ‘daylight saving time’ and is represented by either a ‘1’ for yes or a ‘0’ for no, or possibly ‘-1’ representing an unknown or a default standard.

If you are really new to PHP, here are some resources for PHP novices.

The target date

Let us say our site is about a birthday on 25th September, 2008. We need to specify that as the ‘target’ date as follows:

$target = mktime (0, 0, 0, 9, 25, 2008);

We have left out the DST reference for simplicities sake. Note that the year is a four-digit representation – any two-digit input for year will be interpreted as 2000 onwards from 00 to 69 and 1900 onwards from 70 to 99, hence the advice that four digit years are input.

Directions continue on the next page...

Showing page 1 of 2
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development