Learn How to Create a PHP Countdown Timer

Learn How to Create a PHP Countdown Timer
Page content

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 event 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.

If we wanted to countdown to a party at a particular hour on that day – say 20:00 hours, or 8pm – we would phrase it as follows:

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

Any minutes or seconds that need to be included represent those after the hour prescribed.

However, for our example we will keep it simple, and aim for the beginning of the day.

The Current Date

So, now we have our target date and time, we need to tell the countdown timer what day it is today, and there is a simple function for this:

$today = time () ;

In order for our counter to known how far it has to count, we need to tell it the difference between the target time and now, and again the command is simple:

$difference =($target-$today) ;

Converting from Seconds

It is important to remember that timestamps are measured in seconds, so we need to decide what we are counting down in. A counter can countdown in seconds, minutes, hours or days, and in our example we will aim to countdown in hours.

There are 3600 seconds in an hour, so to convert our timestamp to hours we use the following simple command:

$days =(int) ($difference/3600)

The (int) command is to make sure we are dealing in integers. We now have all of the elements of our timestamp instruction, and it reads as follows:

***<?php

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

$today = time () ;

$difference =($target-$today) ;

$days =(int) ($difference/3600) ;

print “My birthday will begin in $days hours”;

?>***

As we have shown, creating a PHP countdown timer really is a simple task, and one that will add a unique and interesting element to any website or page.

To learn more PHP tips and tricks, check out the following: