繁体   English   中英

PHP 倒计时从日时分秒

[英]PHP Countdown from Day Hour Minute Second

想要显示剩余天数、小时、分钟、秒的倒计时。

时间数据采用以下格式:

开始时间:“2017-02-17T13:51:13”,

我在堆栈中找到了一个示例脚本:

<?php
 $now  = new DateTime();
 $ends = new DateTime('May 31, 2017, 11:59 pm'); 
 $left = $now->diff($ends);
?>


<dd>
<div class="timer_wrp">Remaining
    <ul class="timer countdown">
        <li><i><?php echo $left->format('%a'); ?></i>Day</li>
        <li><i><?php echo $left->format('%h'); ?></i>Hour</li>
        <li><i><?php echo $left->format('%i'); ?></i>Min</li>
        <li><i><?php echo $left->format('%s'); ?></i>Sec</li>
    </ul>
</div>

日期格式不同,是否有一种简单的方法可以使用它当前的设置方式,或者我是否必须将其更改为示例格式? 您是否看到此示例中可能缺少的任何内容? 谢谢

检查这个。 如果您不设置时区,您的计算可能会出错。 因为,默认情况下它采用服务器时区。 它可能有其他解决方案,但就我而言,我总是使用 javascript。

<?php

date_default_timezone_set("Asia/Calcutta");
$now  = new DateTime();
$ends = new DateTime('Oct 9, 2020, 4:40:00'); 
$left = $now->diff($ends);
?>


<div class="timer">

<p>Remaining </p>
<ul class="timer countdown">
    
    <li><i><?php echo $left->format('%a'); ?></i>Day</li>
    <li><i><?php echo $left->format('%h'); ?></i>Hour</li>
    <li><i><?php echo $left->format('%i'); ?></i>Min</li>
    <li><i><?php echo $left->format('%s'); ?></i>Sec</li>
    
</ul>
</div>

你可能也需要这个。 这是用javascript编写的。 复制两者并查看差异。

<p id="demo"></p>
<!--javascript-->
<script>
// Set the date we're counting down to
var countDownDate = new Date("Oct 9, 2020 4:40:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get today's date and time
var now = new Date().getTime();

// Find the distance between now and 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("demo").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("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM