首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php计时器倒计时对此代码的帮助

php计时器倒计时对此代码的帮助
EN

Stack Overflow用户
提问于 2015-12-10 13:55:16
回答 1查看 404关注 0票数 0

有这个我在google搜索过程中发现的 php倒计时代码--这是很好的,我添加到其中的代码,也就是,12月14日,,在这种格式下,没有时间使用它。

如何检查日期是否已过并过期。我想让它像这样展现出来

如果xxxxx日期,则计算其他过期日期:0

代码语言:javascript
复制
$dt_end = new DateTime($expire_date);
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 14:29:31

已更新

试试这个(基于Armen建议的帖子)

代码语言:javascript
复制
<?php
    $expire_date = "dec 14";   //you can change this date
    $datetime1 = new DateTime();  
    $datetime2 = new DateTime($expire_date);

    if($datetime2 >= $datetime1) { 
        $interval = $datetime1->diff($datetime2); 
        $remaining_days = $interval->format('%a');
        $remaning_hours = $interval->format('%h');
        $remaning_minutes = $interval->format('%m');
        $remaning_seconds = $interval->format('%s');
        echo 'Now is '.date('l jS \of F Y h:i:s A').'<br>';
        echo 'To '.$expire_date.' remains ';        
        echo $remaining_days. ' days, '.$remaning_hours.' hours, '.$remaning_minutes.' minutes, '.$remaning_seconds.' seconds.';
    } else {
        echo 'Time expired';
    };
?>

我已经用你最后的请求更新了代码。现在试试..。它会盖上这样的邮票:

现在是2015年12月11日星期五下午06:08:02 截至12月14日,仍有2天、5小时、0分钟、58秒。

包括实时倒计时

为了包括实时倒计时,我们可以使用jQuery和一个天生的脚本来实现这个功能。非常简单,您只需在代码中包含jQuery库,然后在jQuery库jquery.countdown.min.js中添加更多。

您可以从这里下载该库:http://hilios.github.io/jQuery.countdown/,然后将其上传到the服务器上页面的同一文件夹中。

在这里,更新了页面的代码:

代码语言:javascript
复制
    <?php
    $expire_date = "dec 25";   //you can change this date
    $datetime1 = new DateTime();  
    $datetime2 = new DateTime($expire_date);

    $formatted_date = $datetime2->format('Y-m-d'); // we need to pass a well formatted expire date to jQuery


    if($datetime2 >= $datetime1) { 
        $interval = $datetime1->diff($datetime2); 
        $remaining_days = $interval->format('%a');
        $remaining_hours = $interval->format('%h');
        $remaining_minutes = $interval->format('%m');
        $remaining_seconds = $interval->format('%s');
        echo 'Now is '.date('l jS \of F Y h:i:s A').'<br>';

        echo 'To Christmas '.$expire_date.' remains ';        
        echo $remaining_days. ' days, '.$remaining_hours.' hours, '.$remaining_minutes.' minutes, '.$remaining_seconds.' seconds.';
        ?>


        <h2>To Christmas <span id="getting-started"></span></h2> <!-- that is the container of the countdown in your page, jQuery will populate it -->

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- copy this line as it is, it's load jQuery library from google repository -->
        <script src="jquery.countdown.min.js"></script> <!-- here you have to load the countdown library, modify your path in case you'll put that library in a subfolder -->

        <!-- that's our jQuery script -->
        <script type="text/javascript">
                var dateVar = "<?php echo $formatted_date;?>";
                var expire_date=new Date(dateVar);

                $('#getting-started').countdown(expire_date, function(event) {
                    $(this).html(event.strftime('%w weeks %d days %H:%M:%S'));
                });
        </script>

<?php
    } else {
        echo 'Time expired';
    };
?>

输出

现在是2015年12月16日星期三上午07:50:52 到12月25日圣诞节,剩下8天,16小时,0分,8秒。<--静态文本 至圣诞节01周01天17:08:48 <--现场倒计时

希望能有所帮助。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34203709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档