如何将php时间戳与jquery进行比较



我有:

$_SESSION['CREATED'] = time(); 

在php中。

我正在尝试将它"注入"到jquery:

var sesstime = <?php echo json_encode($_SESSION['CREATED']) ?>;

现在我要当前日期:

var timestampjq = (new Date()).getTime();

如何设置这些日期的格式?

我想使用:

if(timestampjq - sesstime > 60) {
}

还是胡说八道?

JS-getTime((

The getTime() method returns the number of milliseconds* since the Unix Epoch.
JavaScript uses milliseconds as the unit of measurement, whereas Unix Time is in seconds.
getTime() always uses UTC for time representation. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

PHP-time((

返回自Unix大纪元(1970年1月1日00:00:00 GMT(。

https://www.php.net/manual/en/function.time.php

计算

因此,基本上,您可以进行计算,只需记住JS以毫秒为单位,PHP时间以秒为单位,所以只需将PHP的值乘以1000即可。

if(timestampjq - sesstime*1000 > 60) {

重要提示:服务器的时间和浏览器的时间可能有差异。

来自PHP文档:

[时间((]返回自Unix Epoch 以来以秒数测量的当前时间

来自getTime:的MDN文档

JavaScript使用毫秒作为度量单位,而Unix时间则以秒为单位。

您可以使用PHP的微时间函数作为UNIX日期。PHP的TimeStamp以微秒为单位返回。

Javascript getTime((函数以毫秒为单位返回UNIX日期。

然后您需要添加";000";到您的PHP时间戳,如果您想使用它,例如:

$yourPHPTimeStamp=圆形(微时间(true(*1000(;

无论如何,我等着帮你进去;(

最新更新