i need a help in implementing a php, javascript stop watch for 4 pages
the total time for stopwatch is 30 minutes when it starts on the next page it should start from the time remaining from previous page and so forth even if it returns to the previous page it should carry on from the time elapsed . At the moment my stopwatch is not linked to other pages it get started from 0 not from the previous page time please help thanks in advance

$dateFormat = "d F Y -- g:i a";
 $targetDate = time() + (2*120);
 $actualDate = time();
 $secondsDiff = $targetDate - $actualDate;
 $remainingDay     = floor($secondsDiff/60/60/24);
 $remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
 $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
 $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
 $actualDateDisplay = date($dateFormat,$actualDate);
 $targetDateDisplay = date($dateFormat,$targetDate);
//include "11.php";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quiz System</title>

<script type="text/javascript">
  var days = <?php echo $remainingDay; ?>  
  var hours = <?php echo $remainingHour; ?>  
  var minutes = <?php echo $remainingMinutes; ?>  
  var seconds = <?php echo $remainingSeconds; ?> 
function setCountDown ()
  if (seconds < 0){
      seconds = 59
  if (minutes < 0){
      minutes = 59
  if (hours < 0){
      hours = 23
  document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
  SD=window.setTimeout( "setCountDown()", 1000 );
  if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
                //window.alert("Time is up. Test Is Finished."); 
                window.location = "www.devnetwork.net" 


<body onload="setCountDown();">

 Start Time: <?php echo $actualDateDisplay; ?><br />
 End Time:<?php echo $targetDateDisplay; ?><br />
 <div id="remain"><?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?></div>

  <?php echo '<a href=11.php>Click here</a>';?>
4 Months
Discussion Span
Last Post by diafol

You should have storage, e.g. DB to store the start time for a particular "test". Then any page reload / new tab / js injection etc can be thwarted.

You can use a js countdown once the server has provided the initial amount of time remaining. If somebody wants to be clever and interfere with the js countdown and give themselves more time, it won't matter as the server time will show that the time has elapsed and not accept anything that is late.

If you go down this route, I'd suggest giving some freedom, e.g. allow submissions to be accepted up 10 seconds late to account for laggy data uploads to server.


i will be very grateful if you could explain some code


Loads of ways you could do this. Depends on your DB setup (basic example):

tests:   id (PK) | testname | duration
test_questions: id | test_id (FK) | (other_fields?)
results: test_id (PK/FK) | user_id (PK/FK) | start_unixtime (other fields?)
users:   id (PK) | (other fields)

You could get data into php variables and apply them to your js variables:

    var countdown = <?=$remainingTime?>;
    //...rest of contdown code...
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.