Skip to main content

How to create quiz timer (Left time) in PHP?

Create quiz timer in PHP. 

<!DOCTYPE html>

<html lang="en">

<body>

<center>

<div class="time" id="navbar">Time left :<span id="timer"></span></div>

<button class="button" id="mybut" onclick="myFunction()">START QUIZ</button>

</center>

<div id="myDIV" style="padding: 10px 30px;">

<form action="result.php" method="post" id="form"> 


</form>

</div>

<script>

function myFunction() {

var x = document.getElementById("myDIV");

    var b = document.getElementById("mybut");

    var x = document.getElementById("myDIV");

if (x.style.display === "none") { 

b.style.visibility = 'hidden';

x.style.display = "block";

startTimer();

}

}

window.onload = function() {

  document.getElementById('myDIV').style.display = 'none';

};

</script>

<?php

$settime = 0 . ':' . 01 . ':' . 06;

?>

 <script type="text/javascript">

document.getElementById('timer').innerHTML = '<?php echo $settime; ?>';

  //06 + ":" + 00 ;

function startTimer() {

  var presentTime = document.getElementById('timer').innerHTML;

  var timeArray = presentTime.split(/[:]+/);

  var h = timeArray[0];

  var m = timeArray[1];

  var s = checkSecond((timeArray[2] - 1));

  if(s==59){

  m=m-1

   if(m==0 && h>0){

h=h-1

m=59

  } 

  }

    document.getElementById('timer').innerHTML =

    h + ":" + m + ":" + s;

  if(h==0 && m==0 && s==0){

  //document.getElementById("form").submit();

  console.log('Time is Done.');

  if(s==0){

return true;

  }

  }

 setTimeout(startTimer, 1000);

}

function checkSecond(sec) {

  if (sec < 10 && sec >= 0) {sec = "0" + sec}; // add zero in front of numbers < 10

  if (sec < 0) {sec = "59"};

  return sec;

 // if(sec == 0 && m == 0){ alert('stop it')};

}

function checkmintue(min) {

  if (min < 10 && min >= 0) {min = "0" + min}; // add zero in front of numbers < 10

  if (min < 0) {min = "59"};

  return min;

 // if(min == 0 && h == 0){ alert('stop it')};

}

</script>

</body>

</html>

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...