Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:09:45+00:00 2026-06-07T00:09:45+00:00

I am creating an online exam page for my project. I have a countdown

  • 0

I am creating an online exam page for my project. I have a countdown timer, but it resets on page refresh. How can I make it not to reset?
The timer is set by fetching time from db. I am using php mysql. Please help me.
Here is my code.

<?php
   $result=mysql_query("select * from test where testid='29'");
   while($time=mysql_fetch_array($result)){
      $dateFormat  = "d F Y -- g:i a";
      $targetDate  = time() + ($time['duration']*60);
      $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);
   }
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</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 ()
      {
          seconds--;
          if (seconds < 0){
             minutes--;
             seconds = 59
          }
          if (minutes < 0){
              hours--;
              minutes = 59
          }
          if (hours < 0){
              hours = 23
          }
          document.getElementById("remain").innerHTML = "  "+hours+" hr "+minutes+" min    "+seconds+" sec";
          SD=window.setTimeout( "setCountDown()", 1000 );
          if (minutes == '00' && seconds == '00') { 
              seconds = "00"; window.clearTimeout(SD);
              window.location = "result.php"
          } 

       }
    </script>

</head>
<body onLoad="setCountDown();">
   <div id="remain">
         <?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?>
   </div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:09:46+00:00Added an answer on June 7, 2026 at 12:09 am

    You should read the value into session variable and using that afterwards:

    <?php
    session_start();
    if (isset($_SESSION['targetdate'])) {
        // session variable_exists, use that
        $targetDate = $_SESSION['targetdate'];
    } else {
        // No session variable, red from mysql
        $result=mysql_query("select * from test where testid='29' LIMIT 1");
        $time=mysql_fetch_array($result);
        $dateFormat = "d F Y -- g:i a";
        $targetDate = time() + ($time['duration']*60);
        $_SESSION['targetdate'] = $targetDate;
    }
    
    $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);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an online game in PHP where users can create playable characters. Each
I am creating an online enrollment form. When a user advances to page 2,
I am creating an online service where special users can upload their images. It
I'm creating an online dictionary and I have to use three different dictionaries for
I'm creating an online music player using php and javascript and I can't figure
I'm creating an iPhone app which is going to have an online spreadsheet to
I am creating a web application where users can upload/download/view online pdfs. I want
I am creating timed online test in C# asp.net. I have created it using
Which modules can be used for creating an online store in drupal?
I'm creating a simple online multiplayer game, with which two players (clients) can play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.