Question

Im creating a website for my brothers wedding. And so far all is going well. However, he wants a countdown to the wedding on the homepage;

Time left until the wedding: X months, X days, X hours.

I would preferebly like to do this using php, but would be open to other suggestions.

if you can help me with ideas for the coding, or just point me to relevant material, that would be useful.

The wedding is on Saturday 30th July.

Was it helpful?

Solution

If your need your counter to be displayed only on page refresh and be static once the page is loaded, then PHP will be fine.

If you need the countdown to get refreshed when the page is displayed, you'll need to use JavaScript.

Personally I would go for something already implemented, like that small script.

OTHER TIPS

For Static Countdown :

//A: RECORDS TODAY'S Date And Time
$today = time();

//B: RECORDS Date And Time OF YOUR EVENT
$event = mktime(0,0,0,12,25,2006);

//C: COMPUTES THE DAYS UNTIL THE EVENT.
$countdown = round(($event - $today)/86400);

//D: DISPLAYS COUNTDOWN UNTIL EVENT
echo "$countown days until Christmas";

?>

Following is the code snippet I have copied from the W3Schools website, while added my PHP code to get the "countdown to" timestamp and the "now" timestamp.

You will see I have commented out the original JavaScript code and replaced it with PHP code in two places, so it's clear to tell what's the difference between two options.

Basically when you think the "server time" is more reliable in your case, you can adopt the PHP approach.

<!DOCTYPE HTML>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    p {
        text-align: center;
        font-size: 60px;
        margin-top: 0px;
    }
    </style>
</head>

<body>
    <p id="demo"></p>
    <script>
    // Set the date we're counting down to
    // 1. JavaScript
    // var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
    // 2. PHP
    var countDownDate = <?php echo strtotime('Sep 5, 2018 15:37:25') ?> * 1000;
    var now = <?php echo time() ?> * 1000;

    // Update the count down every 1 second
    var x = setInterval(function() {

        // Get todays date and time
        // 1. JavaScript
        // var now = new Date().getTime();
        // 2. PHP
        now = now + 1000;

        // Find the distance between now an the count down date
        var distance = countDownDate - now;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Output the result in an element with id="demo"
        document.getElementById("demo").innerHTML = days + "d " + hours + "h " +
            minutes + "m " + seconds + "s ";

        // If the count down is over, write some text 
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("demo").innerHTML = "EXPIRED";
        }
    }, 1000);
    </script>
</body>

</html>

I would not use php (serverside) for this. Because you need to refresh your page every time to see the counting. Preferably use javascript (clientside) for this, more specific jquery (javascript framework). And look for a jquery plugin such as: http://keith-wood.name/countdown.html

If you want something realtime, you'll need to use client-side scripting, namely JavaScript.

You can do it in PHP, but it won't "animate":

$wedding = strtotime("2011-07-01 12:00:00+0400"); // or whenever the wedding is
$secondsLeft = $wedding - time();
$days = floor($secondsLeft / 60*60*24);
$hours = floor(($secondsLeft - $days*60*60*24) / 60*60);
echo "$days days and $hours hours left";

You could put some more math in for months, but it gets fuzzier because a month isn't a fixed amount of time.

The other way would be to use the date() function to pick out the individual elements (hour, minute, second, day, month etc) and use rollover math, but it's a lot of bother for a 'nice effect'.

Let me know if you want a JavaScript example. Don't bother with jQuery - it's a canon for killing a mosquito :)

try this

<?php

    $target = mktime(0, 0, 0, 9, 25, 2011) ;//set marriage date

    $today = time () ;

    $difference =($target-$today) ;

    $month =date('m',$difference) ;
    $days =date('d',$difference) ;
    $hours =date('h',$difference) ;

    print $month." month".$days." days".$hours."hours left";

    ?>

Try this

$wedding = strtotime("2014-02-20 12:00:00"); // or whenever the wedding is
$current=strtotime('now');
$diffference =$wedding-$current;
$days=floor($diffference / (60*60*24));

echo "$days days left";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top