-1

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..

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>
Fido
  • 53
  • 5

1 Answers1

0

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

var endTime = new Date("<?php echo $mysqldate; ?>");
cjs1978
  • 477
  • 3
  • 7
  • Notice that this will of course only work if the PHP variable is available on the same page as the JS (and is set before the JS). – cjs1978 May 31 '18 at 13:56
  • I have doing the same thing with echo but the result is "NaN" – Fido May 31 '18 at 13:57
  • Well, i removed the strtotime and doing as you mentioned and i see now is working, the problem was on strtotime.. – Fido May 31 '18 at 14:01
  • I forgot the quotes around the date. Edited. – cjs1978 May 31 '18 at 14:02
  • After i write GMT+02:00 , and is working just fine. – Fido May 31 '18 at 14:05
  • I can't quite explain that. The format should be new Date("YYYY-MM-DD"), not new Date (YYYY-MM-DD). I.e. this works: var endTime = new Date("2012-01-01 00:00:00 GMT+02:00"); but not var endTime = new Date(2012-01-01 00:00:00 GMT+02:00); – cjs1978 May 31 '18 at 14:08