Search code examples
phpjquerymysqlcountdown

jQuery Countdown with use of PHP & MySQL


The idea: To create a countdown to show days or hours left until some event (no minutes or seconds are required)...

I have MySQL database which stores data used by countdown: id, date & description. I have a PHP file to get it all from database and output it into HTML

How it looks:

HTML Output:

<span>2 Days</span> Until Some Event!

PHP Code:

$result = mysql_query("SELECT * FROM countdowns ORDER BY id DESC",$connect);
while($myrow = mysql_fetch_assoc($result))
{
    echo "<div class=\"countdown\">";
        echo "<span>";
            echo $myrow['date'];
        echo "</span> ";
        echo $myrow['description'];
    echo "</div>";

So I want 'span' to dispaly days OR hours and rest will be description pulled from the database.

The problem: I want it to display ONLY days if hours are greater than 48 or ONLY hours if there are less than 48 hours left.

So it should look like:

5 Days Until Some Event! (There are more than 48 hours left)

OR

5 Hours Until Some Event! (There are obviously less than 48 hours left)

Why I need jQuery for this is because the page on which this countdown will be shown, will be active for days without reloading, so I need it to be dynamic...

I hope you can understand what I mean and probably could help me, thank you very much in advance!

Andrew Badger


Solution

  • Maybe you could use a plugin like timeago and change it so that instead of writing the time eladsed from a timestamp it shows the time missing to a timestamp.

    EDIT - maybe you don't need to change any part of the code but just set this settings

    jQuery.timeago.settings.allowFuture = true;