0

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

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

例如:我在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>

任何帮助将不胜感激。

4

1 回答 1

2

13:00:00 PM不是有效时间。AMPM用于指示时间在 12 小时周期的哪一侧。从逻辑上讲,您不能说您处于时钟 12 小时的第 13 小时。

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

于 2013-09-18T12:13:56.933 回答