如何将 php 中的 strtotime 转换为 js 中的新日期

how to convert strtotime in php to new Date in js

对于实时倒计时,我使用了一些 php 变量并在 js 中回显它们,如下所示:

$expire_year = '2016';  // year
$expire_month = '8'; // month ( 8 = August)
$expire_day = '14'; // day (14th)
$expire_hour = '2'; // hour ( 1-24)
$expire_minutes = '12'; // minutes 

我在 js 中使用它们就像 blow:

timestamp = new Date(<?php echo $expire_year; ?>, <?php echo $expire_month - 1; ?>, <?php echo $expire_day; ?>, <?php echo $expire_hour; ?>, <?php echo $expire_minutes; ?>),

这很好用!

我用phpstrtotime检查日期是否真的过期了:

 $quickpollexpiredate = strtotime("August 14, 2016 2:12"); // set an expiration date

所以对于 js 中的倒数计时器和签入 php,我必须使用 2 个不同的日期 "structures"。

是否可以在 php 中进行检查,以便我只需要将 5 个变量($expire_year、$expire_month...)与 strtotime() 一起使用还是其他功能?

所以实际上我想要实现的是这样的(当然这是错误的,但我希望你能理解我想要实现的目标)

$quickpollexpiredate = strtotime($expire_year, $expire_month, $expire_day, $expire_hour, $expire_minutes); // set an expiration date

如果您在 php 中有一个时间戳,您可以轻松地使用它在 javascript 中设置一个 Date 对象。您只需要考虑 javascript 中的时间戳以毫秒为单位,而不是 php:

中的秒
jsTimestamp = new Date(<?php echo $phpTimestamp; ?> * 1000);

jsTimestamp = new Date(<?php echo strtotime($yourDateString); ?> * 1000);

请注意 new Date(...) 也接受字符串,有关详细信息,请参阅 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date