Javascript / PHP倒数计时器在某个时间停止?

时间:2013-09-18 11:52:59

标签: javascript php mysql

我有倒数计时器的代码。它基本上是PHP / Javascript倒计时器的组合,它将从Mysql表/字段中获取$end_date

问题是它会在某个时间自动停止(这是不需要的)。

例如:我在mysql数据库中将$end_date设置为September 19 2013 11:30:00 AM GMT

倒计时开始并正常工作并开始倒计时。但是,当倒计时器到达September 19 2013 13:00:00 PM GMT时,它将停止并显示Times Up消息!基本上,一旦$end_date更改为13:00:00 PM,它将停止工作或倒计时。

我在代码中看不到会导致此问题的任何内容。除此之外:

if ($now < $exp_date ) {
?>

但同样,这一行只告诉脚本何时开始计数,到目前为止,只要计时器未到达$end_date,我就不会停止倒数计时器停止。或者我错过了什么?

这是我的代码:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php date_default_timezone_set('GMT'); ?>
<?php
session_start();
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php";
$dynamicList = "";
$sql = "SELECT * FROM item ORDER BY id";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $date_added = date("Y-m-d", strtotime($row["date_added"]));
             $end_date = date("F d Y H:i:s A T", strtotime($row["end_date"]));
             $price = $row["price"];
             $dynamicList .= '<div>' . $end_date . '
      </div>';
    }
} else {
    $dynamicList = "No Records";
}
?>

<?php
$date = $end_date;
$exp_date = strtotime($date);
$now = time();

if ($now < $exp_date ) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end = <?php echo $exp_date; ?> * 1000;
var server_now = <?php echo time(); ?> * 1000;
var client_now = new Date().getTime();
var end = server_end - server_now + client_now; // this is the real end time

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24
var timer;

function showRemaining()
{
    var now = new Date();
    var distance = end - now;
    if (distance < 0 ) {
       clearInterval( timer );
       document.getElementById('countdown').innerHTML = 'EXPIRED!';

       return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor( (distance % _day ) / _hour );
    var minutes = Math.floor( (distance % _hour) / _minute );
    var seconds = Math.floor( (distance % _minute) / _second );

    var countdown = document.getElementById('countdown');
    countdown.innerHTML = '';
   if (days) {
        countdown.innerHTML += 'Days: ' + days + '<br />';
    }
    countdown.innerHTML += 'Hours: ' + hours+ '<br />';
    countdown.innerHTML += 'Minutes: ' + minutes+ '<br />';
    countdown.innerHTML += 'Seconds: ' + seconds+ '<br />';
}

timer = setInterval(showRemaining, 1000);
</script>
<?php
} else {
    echo "Times Up";
}
?>
<div id="countdown"></div>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

13:00:00 PM不是有效时间。 AMPM用于指示12小时周期的哪一侧是时间。你不能逻辑地说你正处于12小时的第13个小时。

编辑:为清晰起见:13:00 == 1 PM,13:00 PM ==没有。