如何使用PHP进行倒计时

时间:2010-12-22 11:01:55

标签: php web countdown

我正在为我的兄弟婚礼创建一个网站。到目前为止一切进展顺利。但是,他希望在主页上举行婚礼倒计时;

婚礼前的时间:X个月,X天,X个小时。

我更愿意使用php来做这件事,但会对其他建议持开放态度。

如果你可以帮我提供编码的想法,或者只是指出相关材料,那将是有用的。

婚礼将于7月30日星期六举行。

9 个答案:

答案 0 :(得分:6)

如果您的计数器仅在页面刷新时显示,并且在加载页面后保持静态,那么PHP就可以了。

如果您需要在显示页面时刷新倒计时,则需要使用JavaScript。

就个人而言,我会选择已经实施的内容,例如that small script

答案 1 :(得分:2)

静态倒计时:

//A: RECORDS TODAY'S Date And Time
$today = time();

//B: RECORDS Date And Time OF YOUR EVENT
$event = mktime(0,0,0,12,25,2006);

//C: COMPUTES THE DAYS UNTIL THE EVENT.
$countdown = round(($event - $today)/86400);

//D: DISPLAYS COUNTDOWN UNTIL EVENT
echo "$countown days until Christmas";

?>

答案 2 :(得分:2)

以下是我从W3Schools网站复制的代码段,同时添加了我的PHP代码以获得"倒计时#34;时间戳和"现在"时间戳。

你会看到我已经注释掉了原始的JavaScript代码并在两个地方用PHP代码替换了它,所以很清楚地知道两个选项之间的区别。

基本上,当您考虑服务器时间"在您的情况下更可靠,您可以采用PHP方法。

<!DOCTYPE HTML>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    p {
        text-align: center;
        font-size: 60px;
        margin-top: 0px;
    }
    </style>
</head>

<body>
    <p id="demo"></p>
    <script>
    // Set the date we're counting down to
    // 1. JavaScript
    // var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
    // 2. PHP
    var countDownDate = <?php echo strtotime('Sep 5, 2018 15:37:25') ?> * 1000;
    var now = <?php echo time() ?> * 1000;

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

        // Get todays date and time
        // 1. JavaScript
        // var now = new Date().getTime();
        // 2. PHP
        now = now + 1000;

        // 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("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>
</body>

</html>

答案 3 :(得分:1)


$Target_Date = 1699458858; //expire data
$Time_Left = $Target_Date - time();

$days = floor($Time_Left / (60*60*24));//day
$Time_Left %= (60 * 60 * 24);

$hours = floor($Time_Left / (60 * 60));//hour
$Time_Left %= (60 * 60);

$min = floor($Time_Left / 60);//min
$Time_Left %= 60;

$sec = $Time_Left;//sec

echo "$days days and $hours hours and $min min and $sec sec left";

答案 4 :(得分:0)

我不会为此使用php(服务器端)。因为每次都需要刷新页面以查看计数。最好使用javascript(clientside),更具体的jquery(javascript框架)。并查找jquery插件,例如:http://keith-wood.name/countdown.html

答案 5 :(得分:0)

如果你想要实时的东西,你需要使用客户端脚本,即JavaScript。

你可以在PHP中完成它,但它不会“动画化”:

$wedding = strtotime("2011-07-01 12:00:00+0400"); // or whenever the wedding is
$secondsLeft = $wedding - time();
$days = floor($secondsLeft / 60*60*24);
$hours = floor(($secondsLeft - $days*60*60*24) / 60*60);
echo "$days days and $hours hours left";

你可以在几个月内进行更多的数学计算,但它变得更模糊,因为一个月不是固定的时间。

另一种方法是使用date()函数来挑选单个元素(小时,分钟,秒,日,月等)并使用翻转数学,但是这很麻烦效应”。

如果您需要JavaScript示例,请告诉我。不要打扰jQuery - 它是杀死蚊子的正典:)

答案 6 :(得分:0)

试试这个

<?php

    $target = mktime(0, 0, 0, 9, 25, 2011) ;//set marriage date

    $today = time () ;

    $difference =($target-$today) ;

    $month =date('m',$difference) ;
    $days =date('d',$difference) ;
    $hours =date('h',$difference) ;

    print $month." month".$days." days".$hours."hours left";

    ?>

答案 7 :(得分:0)

试试这个

$wedding = strtotime("2014-02-20 12:00:00"); // or whenever the wedding is
$current=strtotime('now');
$diffference =$wedding-$current;
$days=floor($diffference / (60*60*24));

echo "$days days left";

答案 8 :(得分:0)

$wedding = strtotime("2011-07-01 12:00:00+0400"); // or whenever the wedding is
$secondsLeft = $wedding - time();
$days = floor($secondsLeft / 60*60*24);
$hours = floor(($secondsLeft - $days*60*60*24) / 60*60);
echo "$days days and $hours hours left";

用户Neil E. Pearson编写的代码无效,他忘记在其中写括号(),有效的解决方案是:

$wedding = strtotime("2011-07-01 12:00:00+0400"); // or whenever the wedding is
$secondsLeft = $wedding - time();
$days = floor($secondsLeft / (60*60*24)); // here the brackets
$hours = floor(($secondsLeft - ($days*60*60*24)) / (60*60)); // and here too
echo "$days days and $hours hours left";