想在php中实现倒数计时器

时间:2016-07-11 11:51:12

标签: javascript php html

我在Php页面中使用了countdown timer的以下代码。它运行正常,但问题是它不需要花费数据时间。如果我将20(数据类型时间)插入表并运行Php代码,则需要13小时的时间,这个时间会根据系统时间而变化。我无法理解它的逻辑。

请有人帮忙......!感谢。

 $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);

此处 $ tid 是测试ID,根据ID,它将从数据库中获取时间,我在数据库中为时间提供了时间数据类型。

这是剧本:

<!-- 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"
                  } 

           }

0 个答案:

没有答案