1. Computing

How To Make a Holiday Countdown on Your Website.

By

If you're excited for Christmas (or any other special day) you can make a countdown on your website using PHP. It's very simple! Just follow the steps below.

Difficulty: Easy

Time Required: 2 mins

Here's How:

  1. First we have to set our target date. For the sake of our example, we'll use Christmas. We do that like this:

    $target = mktime(0, 0, 0, 12, 25, 2009) ;

     

  2. We also need to know today's date. We can do that using the time function:

    $today = time () ;

  3. Next we subtract to calculate the difference between our target date (Christmas) and today's date. We do that like this:

    $difference =($target-$today) ;

  4. Since the the difference is measured in seconds, we probably want to convert this into a more friendly measurement. If we want hours we can divide by 3600, however in our example we will be using days so we need to divide by 86400 (the number of seconds in a day.) We also want to make sure our number is an integer, so we will use the tag int.

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

  5. When we put it all together we get our final countdown code:

    <?php
    $target = mktime(0, 0, 0, 12, 25, 2009) ;
    $today = time () ;
    $difference =($target-$today) ;
    $days =(int) ($difference/86400) ;
    print "Christmas is in $days days";
    ?>

    Download this code from GitHub: https://github.com/Goatella/PHPWhatDay

©2014 About.com. All rights reserved.