0

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.

Andreas
  • 23,610
  • 6
  • 30
  • 62
vikram
  • 189
  • 3
  • 13
  • 1
    http://stackoverflow.com/questions/7115620/countdown-timer-built-on-php-and-jquery .. check the correct answer – Alive to die - Anant Jun 21 '16 at 13:00
  • You will need to calculate the difference between the start time and the current time. If you want it to update live, you will either need to refresh the page occasionally, or use JavaScript. Possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – 4castle Jun 21 '16 at 13:05
  • You forgot to mention what the exact problem is. – jeroen Jun 21 '16 at 13:08
  • There are a ton of unknowns here - do you already have an authenticated 'admin' section, a persistent storage mechanism and crud functionality for the admin user? – Steve Jun 21 '16 at 13:08
  • Why don't you use JavaScript? – Alok Patel Jun 21 '16 at 13:08

2 Answers2

1

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

Omkar Frozen
  • 157
  • 9
1
$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
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • @vikram not working? Anything that needs to be changed? – Andreas Jun 22 '16 at 09:30
  • I want a continuous clock, which shows the time decreasing, https://github.com/SeanJA/countdown-clock/blob/master/gif.php ,,, see this person have given one, but there is a bug, it will run properly only for one minute – vikram Jun 23 '16 at 07:16
  • If you want a continuous clock then you should not have taged php. Php only runs once on the server and creates a "html" file. If you want a continuous then you should have taged the question with javascript – Andreas Jun 23 '16 at 07:28
  • I am little unaware of how below concept work. What i want is a page, which is sent to the user as email, in which say i set 24th-june-2016 11am as deadline, it will show them the remaining time in the counter, whenever they open mail. Can we use JS in such pages? Please help – vikram Jun 23 '16 at 07:30
  • @vikram knowing what is js and what is php is important. But lets put that aside. The php code will show the time left but static. So you need to reload the page for it to show the time accuratly. I have updated my answer with a link to jsfiddle with a javascript version that counts down in real time to the date and time. – Andreas Jun 23 '16 at 08:56
  • marking it as answer looking at your enthu sir, though it was not meeting my requirement and understanding it cannot be met also :-) thank You! – vikram Jun 24 '16 at 12:27