I have made a countdown timer using js ajax and php. The problem is this when the timer reaches 00:00 it doesnot perform the specified operation. instead timer starts from 59:59. Here is the code written:
<span class="pull-right" id="time_left"></span>
date_default_timezone_set('Asia/Karachi');
echo $_SESSION['end_time'] = $quiz_end_time;
//timer function
setInterval (function(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","timer.php",false);
xmlhttp.send(null);
document.getElementById("time_left").innerHTML = xmlhttp.responseText;
}, 1000);
//timer.php code
<?php session_start();
$from_time = date('Y-m-d H:i:s');
$to_time = $_SESSION['end_time'];
$time_form = strtotime($from_time);
$time_to = strtotime($to_time);
$difference = $time_to - $time_form;
echo "Time Left: ";echo $timer_time = date("i:s",$difference);echo "<br>";
//echo date ('H:i:s',strtotime($timer_time));
if($timer_time == "00:00"){
?>
<script>
document.getElementById("quiz_form").submit();
//window.location="manage-quizzes.php?Msg=quiz_ended"
</script>
<?php
session_destroy($_SESSION['quiz_id']);
session_destroy($_SESSION['end_time']);
header('Location:manage-quizzes.php?Msg=quiz_ended');
}else{
echo "OKAY";
}
?>