简体   繁体   English

Date()。getTime()JavaScript转换为php吗?

[英]Date().getTime() JavaScript convert to php?

I have this jquery function to get date diference. 我有这个jQuery函数来获取日期差。

  function distance(date) {
    return (new Date().getTime() - date.getTime());
  }

I tried to convert this new date() to php: 我试图将这个新的date()转换为php:

  function distance(date) {
    return (<?php echo time(); ?> - date.getTime());
  }

this way I could get server date instead of user date. 这样我可以获得服务器日期而不是用户日期。 but it is wrong cause the time diference become 49 years. 但这是错误的,因为时间差变为49年。 any ideas why? 有什么想法吗?

In php time() returns the time in seconds. 在php中, time()以秒为单位返回时间。 (in unix time) (在Unix时间)

While in Javascript it is returned in milliseconds. 在Javascript中,它以毫秒为单位返回。

Your solution would be to convert php time in milliseconds. 您的解决方案是将php时间转换为毫秒。

function distance(date) {
  return (<?php echo time()*1000; ?> - date.getTime());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM