Arrow

Date countdown code in php

First published on August 4, 2006

This was the first php script that I ever looked up sooooo many years ago. It’s really quite simple. You want to show visitors a countdown in days to a certain event in the future. Since PHP can provide the current time on the server to the very second, simply calculate the desired date (in seconds) and subtract from the server date. More complicated scripts can give a choice between hours, months, or days remaining, can compensate for a server time different than your current time, and can probably dress up the output a bit better.

I’ve spiced this code up a bit by making it a function (thus enhancing reusability), but otherwise, let’s go with simple, shall we?

Here’s the code:

<?php
function countdown( $event, $month, $day, $year )
{
    // subtract desired date from current date and give an answer in terms of days
    $remain = ceil( ( mktime( 0,0,0,$month,$day,$year ) - time() ) / 86400 );
    // show the number of days left
    if( $remain > 0 )
    {
        print "<p><strong>$remain</strong> more sleeps until $event</p>";
    }
    // if the event has arrived, say so!
    else
    {
        print "<p>$event has arrived!</p>";
    }
}

// call the function
countdown( "Christmas", 12, 25, 2024 );
?>

How about some more fun:

Christmas countdown

Update: I have since made a countdown plugin for WordPress that incorporates the basic concept.

Arrow

11 Responses to “Date countdown code in php”


  1. Julia says:

    Hey pretty cool! This is just what I was thinking I need! How do I use that? Do I just cut and paste that code somewhere?


  2. Philip says:

    Cheers for the code, worked great!


  3. Crystal says:

    I was looking for this exact code. Thank you very much!


  4. Distorzija says:

    This is exactly what I need! Thanks for sharing, mate! Well done.


  5. Mach37 says:

    Hey Cheersfor this code!!! ;) Great blog!
    I will do that into my image how many days remaining to school end :D


  6. David says:

    Awesome! This was very much what I was looking for!
    I’m very new to PHP so I don’t know the answer to this, but is it possible to pass a variable to the function call?

    As in, I have some variables like $fd = get_ept_field(fulfilment_day)
    and I want to pass those into function.

    So I’ve been doing something like

    countdown("Christmas Day", 12, (int)$fd, 2008); to call the function, but it just won’t carry through to the actual function :(

    You wouldn’t happen to know where I’ve gone wrong would you?

    In any case thanks for this function – I’ve been looking for it everywhere!

    Reply from Peter: Yes, that is definitely possible. Do a var_dump( $fd ); to check whether that variable contains what you think it does.


  7. David says:

    OK – I get it – it won’t pass through because it is a string, not an integer. Although all attempts to make it an integer are returning false, which is a major bummer:

    $dayy = the_ept_field(fulfilment_day);
    var_dump($dayy);

    returns: string(29) "10"

    $dnum = (int)$dayy;
    var_dump($dnum);

    returns: int(0)

    (also returns 0 using intval($dayy) )

    Any help is greatly appreciated! Hopefully this advice can help others looking to dynamically add dates to your function!

    Reply from Peter: PHP is quite flexible, so in this case I don’t think it makes a huge difference whether it’s a string or an integer. I’m not sure what the issue is, although the fact that your original value is apparently 29 characters long suggests something fishy. Also, is “fulfilment_day” a string (in which case, it should be enclosed in quotes)? A variable?


  8. David says:

    Hi Peter,

    Yes, my mistake – ‘fulfilment_day’ should have been in quotes! And you were right about the 29 characters being fishy – I didn’t realize, but I was returning a "formatted" version of the string – so instead of:

    $dayy = get_ept_field(‘fulfilment_day’);

    I had to use:

    $dayy = get_ept_field(‘fulfilment_day’, array(‘raw’ => ‘true’) );

    which returned string(2) "11", and could be passed correctly into your function!

    Thanks so much for your help – I’m sorry it wasn’t more of an interesting problem rather than a snafu on my part. Still, I hope this can help someone in the future.


  9. Ralph Frost says:

    Hey, very nice job! Thank you. I like the simplicity of (number of seconds at the future/event date minus the number of seconds right now).

    FWIW, I modified your function to cast from int to a string: $remain = (string)$remain; and then return(ed) $remain; at the end of the function, which loaded it into a var by the calling function. Then I used the string to write on an image with GD functions, fiddlingh with the x,y coordinates based on if strlen($remain) was 2 or 1. GD is a whole other difficult Sudoku puzzle, but I got the thing to work. Thanks again.


  10. Jaco says:

    Very nice!

    Is it possible to convert this to a "count-up" counter as well? I want to count down from today (eg. D-21) to a certain date (D)and then everyday upwards from there (D+x). Any help would be much appreciated!

    Reply from Peter: In the sample code, the $remain value will continue to go into the negative, so you could do a test on that and then output the absolute value of that as the “count up” value.


  11. PN says:

    Thanks for sharing this, just what I was looking for!

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word