Search code examples
javascriptphpcountdownevent

I want to set a countdown time and keep deadline reducing php


Sorry that I am unable to express the question properly.

Basically what I need to do is this.

There should be a php page which shows the countdown timer. Say the admin sets it for 24 hours now and starts. Who ever visits that page, it shows the remaining deadline time. Example, if user visit now, 24 hours remaining,if after 2 hours, users visit,it should say 22 hours remaining. Thank you. (anyone can please edit it to make it understandable).

I want it to update continuously.


Solution

  • $rem = strtotime('2016-06-21 20:00:00') - time(); // change date and time to suit.
    $day = floor($rem / 86400);
    $hr  = floor(($rem % 86400) / 3600);
    $min = floor(($rem % 3600) / 60);
    $sec = ($rem % 60);
    if($day) echo $day. "Days left<br>";
    if($hr) echo $hr. "Hours left<br>";
    if($min) echo $min. "Minutes left<br>";
    if($sec) echo $sec. "Seconds left";
    

    In javascript that would be: https://jsfiddle.net/z4avs7Lx/

    Html:

    <div id="countdown"></div>
    

    Javascript:

    var end = new Date('06/24/2016 11:00 AM');
    
    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;
    
    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {
    
            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'EXPIRED!';
    
            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);
    
        document.getElementById('countdown').innerHTML = days + 'days ';
        document.getElementById('countdown').innerHTML += hours + 'hrs ';
        document.getElementById('countdown').innerHTML += minutes + 'mins ';
        document.getElementById('countdown').innerHTML += seconds + 'secs';
    }
    
    timer = setInterval(showRemaining, 100