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

1.9k views Asked by At

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.

2

There are 2 answers

6
Andreas On BEST ANSWER
$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
0
Omkar Frozen On

It is very much easy, create a variable and store the UNIX timestamp of deadline you want (in your case 24 hours). Now when user opens a site call time(). i.e call this function in your PHP script now with this timestamp as you need to show each second becoming less you need client side language jquery to show the time ticking on the browser. So why do you need to time() from php because users from different locations and incorrect time will not serve the purpose