我正在使用 PHP 从我的数据库中选择日期时间。对于从数据库中选择的每个日期,我想使用 javascript 将剩余的 H:D:M:S 显示到选定的日期时间。我通过在循环 [while($row = mysqli_fetch_assoc($result))] 中回显以下 javascript 代码来做到这一点:
<script>
var countDownDate = new Date("'.$formatted_date.'").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("'.$counter.'").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("'.$counter.'").innerHTML = "EXPIRED";
}
}, 1000);
</script>
然后,我使用 This works fine 回显倒数计时器,<span id="'.$counter.'"></span>
但是无论从数据库中选择的日期数量如何,每个跨度值都是相同的。(我假设它只是计算从数据库中选择的最后一个值而不是每个值的倒计时?)
请问有什么帮助吗?