0

I have use following code for countdown timer in Php page.It's works fine, but the problem is it does not take time from database. If i insert 20 (datatype time) into table and run the Php code it takes 13hr something and this time changes depending on system time. I have failed to understand the logic of it takes.

Please anyone help...! Thanks.

 $t1=mysql_query("select * from mst_test where test_id='$tid'");
    $t2=mysql_fetch_row($t1);

    $_SESSION['targetDate']=$t2[4];
    $targetDate = $_SESSION['targetDate'];
} else {
    // No session variable, red from mysql
    $result=mysql_query("select * from mst_test where test_id='$tid'"); 
    $time=mysql_fetch_row($result); 

    $dateFormat = "d F Y -- g:i a";
    $targetDate = $time['time'];
    $_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);

Here $tid is Test id, based on the Id it will fetch the time from database and i gave time datatype for time in database.

This is the script:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    <div id='timer'>
     <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 = "review.php"
                  } 

           }
  • What do you mean if you insert `20`? 20 where? – Fredster Jul 11 '16 at 11:56
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 11 '16 at 12:00
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 11 '16 at 12:00
  • 20 into database table –  Jul 11 '16 at 12:05

0 Answers0