倒数计时器未显示
countdown timer isn't showing up
我试图用 php 和 js 创建倒计时。
我正在学习 js 和 php,所以我想尝试将这两者结合起来进行倒计时,其中包括天、小时、分钟和秒。这段代码的结果只告诉我它运行到什么时候,但没有告诉我还剩多少时间,所以倒计时本身是行不通的。有人可以帮忙吗?
<?php
$date = date('2022-02-26');
$time = date('23:59:59');
$date_today = $date . ' ' . $time;
echo "it will run till" .$date_today;
?>
<script type="text/javascript">
//set the date we are counting to
var count_id = "<?php echo $date_today; ?>";
var countDownDate = new Date(count_id).getTime();
//update countdown every second
var x = setInterval(function(){
//get today's date and time
var now = new date().getTime();
//find the distance between now and countdown 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 results in an elemtwith id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the countdown over, write some text
if(distance<0){
clearInterval(x);
document.getElementById("demo").innerHTML="Expired"
}
},1000);
</script><?php
echo '<p id="demo" style="font-size:30px;"></p>';
?>
var now = new date().getTime();
拼写错误,应该是
var now = new Date().getTime();
现在可以使用了
我试图用 php 和 js 创建倒计时。 我正在学习 js 和 php,所以我想尝试将这两者结合起来进行倒计时,其中包括天、小时、分钟和秒。这段代码的结果只告诉我它运行到什么时候,但没有告诉我还剩多少时间,所以倒计时本身是行不通的。有人可以帮忙吗?
<?php
$date = date('2022-02-26');
$time = date('23:59:59');
$date_today = $date . ' ' . $time;
echo "it will run till" .$date_today;
?>
<script type="text/javascript">
//set the date we are counting to
var count_id = "<?php echo $date_today; ?>";
var countDownDate = new Date(count_id).getTime();
//update countdown every second
var x = setInterval(function(){
//get today's date and time
var now = new date().getTime();
//find the distance between now and countdown 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 results in an elemtwith id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h " +
minutes + "m " + seconds + "s ";
// If the countdown over, write some text
if(distance<0){
clearInterval(x);
document.getElementById("demo").innerHTML="Expired"
}
},1000);
</script><?php
echo '<p id="demo" style="font-size:30px;"></p>';
?>
var now = new date().getTime();
拼写错误,应该是
var now = new Date().getTime();
现在可以使用了