我为我兄弟的婚礼创建了一个网站。到目前为止,一切进展顺利。但是,他想在主页上进行婚礼倒计时;
距离婚礼还剩:X 个月,X 天,X 小时。
我更愿意使用 php 来执行此操作,但愿意接受其他建议。
如果您可以帮助我提出编码的想法,或者只是将我指向相关材料,那将很有用。
婚礼于 7 月 30 日星期六举行。
如果您需要您的计数器仅在页面刷新时显示并且在页面加载后保持静态,那么 PHP 就可以了。
如果您需要在页面显示时刷新倒计时,则需要使用 JavaScript。
就我个人而言,我会选择已经实现的东西,比如那个小脚本。
对于静态倒计时:
<?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";
?>
以下是我从W3Schools网站复制的代码片段,同时添加了我的 PHP 代码以获取“倒计时”时间戳和“现在”时间戳。
你会看到我已经注释掉了原始的 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>
$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";
$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";
我不会为此使用php(服务器端)。因为您需要每次刷新页面才能看到计数。最好为此使用 javascript(客户端),更具体的 jquery(javascript 框架)。并寻找一个 jquery 插件,例如: http: //keith-wood.name/countdown.html
如果你想要一些实时的东西,你需要使用客户端脚本,即 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 烦恼——它是杀死蚊子的经典 :)
尝试这个
<?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";
?>
尝试这个
$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";
试试这个静态倒计时。
<?php
//Current Date And Time
$todaytime = time();
//Set Date And Time OF The Event
$eventdate = mktime(0,0,0,0,28,2021);
//Calculate Days Until The Event
$countdown = round(($eventdate - $todaytime)/86400);
//Display Countdown
echo "$countdown days until the event";
?>