Creating a PHP Countdown Timer

Written by:  • Edited by: Linda Richter
Updated May 24, 2011
• Related Guides: HTML | Wedding | PHP

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 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:


Comments

Showing all 8 comments
 
Lili Jan 25, 2012 11:13 PM
RE: Creating a PHP Countdown Timer
excellent!! many thanks for this!<br>I changed the script to days...<br>wondering what it would be like to add so-many days and so-many hours to something...<br>cheers!
mail@adress.com May 9, 2010 1:04 PM
Thanks
Thanks!
Elysoon Dec 23, 2009 6:51 PM
The Script is somewhat wrong
When I set the target to April 25, 2010, the result is 123 which is 1 day ahead, it should be 122.

But when I set the target to May 1, 2010, the result is 132 which is 3 days ahead, it should be 129.

Is the script wrong? or it just acts like that?
sean Dec 16, 2009 5:29 PM
What about a count up timer?
How would we go about creating a count up timer? Maybe even something where I could append a query string to the end of a url in order to get a certain date....for example, we have this:
http://www.rehabilitation-center.org/rehabwidget.html
but how hard do you think it would be to make it so that if you went to http://www.rehabilitation-center.org/rehabwidget.html?day=12mnth=10year=1983 type thing in order to insert the value? sorry....im a newb
Camotec Aug 29, 2009 7:43 PM
Script is not wrong
The script is not wrong and works fine, however I think a more appropriate referrer should be used ie. instead of $days use $hours for example:

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

& then:

print "My birthday will begin in $hours hours

my 2 cents.
myincome Jul 8, 2009 4:33 PM
displaying countdown
It is better to display countdown using JavaScript so it wil update every second liek here http://free-countdown.co.nr/
wooly Jul 6, 2009 12:49 PM
no its not
It says right in the paragraph above that "There are 3600 seconds in an hour, so to convert our timestamp to hours we use the following simple command:"
Mini-Mog Jun 27, 2009 12:07 PM
Script is wrong
3600 Seconds is not a day !
60 Seconds = 1 minute
60 Minute = 1 Hour
=> 60 x 60 seconds = 1 hour
24 hours = 1 day
=> 60 x 60 x 24 = 86400 seconds = 1 day
 
blog comments powered by Disqus
Email to a friend