0

I am making a php timer that i refresh with ajax every second on my page. But i am having problems getting it to work. I need it to countdown 8 minutes. This is the script

<?php
$saveTime = (3600*10); // Saved time from file/database
$thisTime = time(); // Current time
$diffTime = ($saveTime-$thisTime); // Difference in time
if($diffTime >= 1) {
    $countMin = floor($diffTime/60);
    $countSec = ($diffTime-($countMin*60));
    echo 'Time remaining until next run is in ',$countMin,' minute(s) ',$countSec,' seconds';
} else {
    echo 'Timer expired.';
}
?>
3
  • 2
    But I am having problems getting it to work. Please elaborate Commented Jan 30, 2014 at 7:36
  • It simply prints Timer expired. So the problem must be happening in the $diffTime variable. But I am not sure exactly what. Commented Jan 30, 2014 at 7:37
  • I think your time difference is not correct. Check stackoverflow.com/questions/365191/… ........ Commented Jan 30, 2014 at 7:39

1 Answer 1

1

Your problem is in the value saved in the $saveTime variable.

time() returns the number of seconds since 1st January 1970. Your value for $saveTime is about 10am on that day.

You need to set $saveTime to some time in the future. For example,

$saveTime = time()+(8*60);  // 8 minutes into the future.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.