为什么Jquery倒数计时器不接受PHP日期?

时间:2017-03-04 20:20:31

标签: javascript php jquery mysql

我正在使用Jquery"最终倒计时"计时器(在这里找到:http://hilios.github.io/jQuery.countdown/)但我将其修改为从php var指定的日期倒计时,如下所示:

PHP:

$gametimestamp = date("Y/m/d H:i:s", strtotime($gametimestamp));

Javascript:

var plustime = new Date(<?php echo $gametimestamp; ?>);
plustime.setSeconds(plustime.getSeconds() + 30);

  $("#timerinsert")
  .countdown(plustime, function(event) {
    $(this).text(
      event.strftime('%M:%S')
    );
  });

如果

 var plustime = new Date(<?php echo $gametimestamp; ?>); 

输出如:

var plustime = new Date(2017/03/04 20:19:10);

那为什么倒数计时器没有显示?如果我只是使用

它会工作
date();

1 个答案:

答案 0 :(得分:0)

在将字符串回显到javascript

时,您必须添加引号
var plustime = new Date("<?php echo $gametimestamp; ?>");

请注意new Date接受某种格式,最好是从epoch中回显秒,并添加三个零

$gametimestamp = strtotime($gametimestamp); // Unix timestamp in seconds

然后

var plustime = new Date(<?php echo $gametimestamp; ?> * 1000); // in milliseconds
相关问题