从Mysql DB到Javascript计时器的日期 [英] Date from Mysql DB to Javascript timer

查看:109
本文介绍了从Mysql DB到Javascript计时器的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JS中有一个计时器脚本,我想从Mysql DB中获取数据,但是不起作用,这是我到目前为止所做的..

I have a timer script in JS and i want to grab data from Mysql DB but is not working, here is what i do so far..

我来自expirydate数据库的日期格式是数字:"1527886800"

My date format from expirydate DB is numeric: "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>

推荐答案

您不能在JS代码中直接使用PHP变量.您需要执行以下操作:

You cannot use the PHP variable directly in the JS code. You need to do something like:

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

这篇关于从Mysql DB到Javascript计时器的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆