我想为即将发布的帖子显示一个倒数计时器。我正在使用JavaScript和PHP来实现它。我在JavaScript函数中传递PHP变量值。你能看到代码并帮助我吗? 我的代码:
<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>
答案 0 :(得分:0)
你的问题不清楚倒计时应该是多么精细;小时,天等等。
如果您可以提供标准日期格式或javascript时间戳(自1970年1月1日以来的毫秒数),您可以使用MomentJS来确定“相对时间”。使用此相对时间,您可以确定指定日期之前的秒数,分钟数,小时数,天数,周数或月数。有了这个,很容易在字符串前加“剩余”。如:
剩下7天