PHP循环中的Javascript倒计时器

时间:2018-05-17 12:56:19

标签: javascript php

我正在使用PHP从数据库中选择日期时间。对于从数据库中选择的每个日期,我想使用javascript将剩余的H:D:M:S显示到选定的日期时间。我通过在循环中回显以下javascript代码来做到这一点[while($ row = mysqli_fetch_assoc($ result))]:

<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>

然后我使用<span id="'.$counter.'"></span>回显倒数计时器 这样可以正常工作,但无论从数据库中选择的日期数是多少,每个跨度值都是相同的。 (我假设它只计算从db中选择的最后一个值而不是每个值的倒计时?)

请帮忙吗?

0 个答案:

没有答案