我想显示一个倒数计时器为即将到来的职位。我用JavaScript与PHP来实现它。我传递一个PHP变量值在JavaScript函数。你能看到的代码并帮助我吗?我的代码:
<span><i class="fa fa-calendar-o"></i> <?php echo get_the_date();?></span>
<?php $dtimer = get_the_date(); ?>
<span><i class='fa fa-calendar-o'></i><p id='timer-diff'></p></span>
<script>
// Set the date we're counting down to
var countDownDate = new Date("<?php echo $dtimer; ?>").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="timer-diff"
document.getElementById("timer-diff").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("timer-diff").innerHTML = "EXPIRED";
}
}, 1000);
</script>
1条答案
按热度按时间rhfm7lfc1#
你在问题中没有明确倒计时的粒度;小时、天等
如果你能提供一个标准的日期格式,或者一个javascript时间戳(自1970年1月1日起的毫秒数),你可以使用MomentJS来确定“相对时间”。通过这个相对时间,你可以确定到指定日期为止的秒数、分钟数、小时数、天数、周数或月数。这样,就可以很容易地在字符串前面加上“remaining”。例如:
剩余7天