Author Topic: Simple Countdown Timer  (Read 198 times)

K3YL3SS

  • Administrator
  • Developer
  • *****
  • Posts: 113
  • Thanks: 7
    • View Profile
    • Peg Productions Web Design
Simple Countdown Timer
« on: January 17, 2010, 05:47:02 PM »
If your site focuses on a specific event such as Christmas or your wedding, you may want to have a count down to let users know how long it will be until the event occurs. We can do this using timestamps and the mktime function.

Let's get going
First we need to set our target date. For our example we will use February 10th, 2007. We would get that with this line:
$target mktime(0002102007);

Next we need to get the current date. We can do that with this line:
$today time ();

We now have to find the difference between them. To do that we simply need to subtract:
$difference = ($target-$today);

Since the timestamp is measured in seconds, we convert this into whatever units we want. If we want hours we can divide by 3600, however in thisexample we will be using days so we need to divide by 86400 (the number of seconds in a day). Floor simply rounds down.
$days =  floor($difference/86400);

And there we have it!
$target mktime(0002102007);
$today time ();
$difference = ($target-$today);
$days =  floor($difference/86400);
echo 
"The event will occur in $days days";


http://pegfm.pezcuckow.com - PegFM Internet Radio Station
http://www.pegproductions.com - Peg Productions Web Design