如何使用 PHP 进行倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4508488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a countdown using PHP
提问by RSM
Im creating a website for my brothers wedding. And so far all is going well. However, he wants a countdown to the wedding on the homepage;
我正在为我兄弟的婚礼创建一个网站。到目前为止,一切进展顺利。然而,他想在主页上为婚礼倒计时;
Time left until the wedding: X months, X days, X hours.
距离婚礼还有时间:X 个月,X 天,X 小时。
I would preferebly like to do this using php, but would be open to other suggestions.
我更喜欢使用 php 来做到这一点,但会接受其他建议。
if you can help me with ideas for the coding, or just point me to relevant material, that would be useful.
如果您可以帮助我提供编码的想法,或者只是向我指出相关材料,那将会很有用。
The wedding is on Saturday 30th July.
婚礼定于7月30日星期六。
采纳答案by Vincent Mimoun-Prat
If your need your counter to be displayed only on page refresh and be static once the page is loaded, then PHP will be fine.
如果您需要您的计数器仅在页面刷新时显示并在页面加载后保持静态,那么 PHP 就可以了。
If you need the countdown to get refreshed when the page is displayed, you'll need to use JavaScript.
如果您需要在页面显示时刷新倒计时,则需要使用 JavaScript。
Personally I would go for something already implemented, like that small script.
我个人会选择已经实现的东西,比如那个小脚本。
回答by Vincent Mimoun-Prat
For Static Countdown :
对于静态倒计时:
<?php
//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 "$countdown days until Christmas";
?>
回答by hailong
Following is the code snippet I have copied from the W3Schoolswebsite, while added my PHP code to get the "countdown to" timestamp and the "now" timestamp.
以下是我从W3Schools网站复制的代码片段,同时添加了我的 PHP 代码以获取“倒计时”时间戳和“现在”时间戳。
You will see I have commented out the original JavaScript code and replaced it with PHP code in two places, so it's clear to tell what's the difference between two options.
您会看到我已经注释掉了原始 JavaScript 代码并在两个地方用 PHP 代码替换了它,因此很清楚地说明了两个选项之间的区别。
Basically when you think the "server time" is more reliable in your case, you can adopt the PHP approach.
基本上,当您认为“服务器时间”在您的情况下更可靠时,您可以采用 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>
回答by MetropolisCZ
$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";
Code top by user Neil E. Pearson didn′t work, he forgot to write there the brackets (), working solution is:
用户 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";
回答by filsterjisah
I would not use php (serverside) for this. Because you need to refresh your page every time to see the counting. Preferably use javascript (clientside) for this, more specific jquery (javascript framework). And look for a jquery plugin such as: http://keith-wood.name/countdown.html
我不会为此使用 php(服务器端)。因为您每次都需要刷新页面才能看到计数。最好为此使用 javascript(客户端),更具体的 jquery(javascript 框架)。并寻找一个 jquery 插件,例如:http: //keith-wood.name/countdown.html
回答by Neil E. Pearson
If you want something realtime, you'll need to use client-side scripting, namely JavaScript.
如果你想要一些实时的东西,你需要使用客户端脚本,即 JavaScript。
You can do it in PHP, but it won't "animate":
你可以用 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";
You could put some more math in for months, but it gets fuzzier because a month isn't a fixed amount of time.
您可以在几个月内添加更多数学,但它会变得更加模糊,因为一个月不是固定的时间。
The other way would be to use the date()
function to pick out the individual elements (hour, minute, second, day, month etc) and use rollover math, but it's a lot of bother for a 'nice effect'.
另一种方法是使用该date()
函数来挑选单个元素(小时、分钟、秒、日、月等)并使用翻转数学,但对于“好的效果”来说很麻烦。
Let me know if you want a JavaScript example. Don't bother with jQuery - it's a canon for killing a mosquito :)
如果您需要 JavaScript 示例,请告诉我。不要理会 jQuery - 它是杀死蚊子的经典 :)
回答by Bhanu Prakash Pandey
try this
尝试这个
<?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";
?>
回答by Rejayi CS
Try this
尝试这个
$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";