从mysql db到javascript的日期计时器

uxhixvfz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(357)

我在js中有一个计时器脚本,我想从mysql数据库中获取数据,但不起作用,下面是我目前所做的。。
我的日期格式来自 expirydate db是数字:“1527886800”
PHP:

<?php
       $db = mysqli_connect("localhost","root","pass","db");
    if (!$db) {
        die ('Can\'t use db: ' . mysqli_error());
    }
         $qt = mysqli_query($db,"SELECT expirydate FROM licenses ");
     while ($row = mysqli_fetch_assoc($qt)) {
      $expire = $row['expirydate'];
      $expire = strtotime( $expire );
      $mysqldate = date( 'Y-m-d H:i:s', $expire );
}
     ?>

js公司:

<script>
         function makeTimer() {

                    var endTime = new Date($mysqldate);

                    endTime = (Date.parse(endTime) / 1000);

                    var now = new Date();
                    now = (Date.parse(now) / 1000);

                    var timeLeft = endTime - now;

                    var days = Math.floor(timeLeft / 86400);
                    var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
                    var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600)) / 60);
                    var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

                    if (hours < "10") { hours = "0" + hours; }
                    if (minutes < "10") { minutes = "0" + minutes; }
                    if (seconds < "10") { seconds = "0" + seconds; }

                    $(".days").html(days + "<span> Zile</span>");
                    $(".hours").html(hours + "<span> Ore</span>");
                    $(".minutes").html(minutes + "<span> Minute</span>");
                    $(".seconds").html(seconds + "<span> Secunde</span>");

                }

                setInterval(function () { makeTimer(); }, 1000);
    </script>
pw136qt2

pw136qt21#

不能在js代码中直接使用php变量。你需要做一些类似的事情:

var endTime = new Date("<?php echo $mysqldate; ?>");

相关问题