简体   繁体   English

倒数计时器未显示

[英]countdown timer isn't showing up

I was trying to create a countdown with php and js.我试图用 php 和 js 创建一个倒计时。 I am learning js and php at the moment so I wanted to try and combine these two to make a countdown in which days, hours, minutes and seconds are included.我正在学习 js 和 php,所以我想尝试将这两者结合起来进行倒计时,其中包括天、小时、分钟和秒。 the outcome of this code only tells me till when it runs but not how much time is left, so the countdown itself isn't working.这段代码的结果只告诉我它运行到什么时候,但没有告诉我还剩多少时间,所以倒计时本身是行不通的。 can anyone help?谁能帮忙?

  <?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();

spelling mistake, it should be拼写错误,应该是

var now = new Date().getTime();

and now it works现在它起作用了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM