0

此 PHP 代码是否有 Javascript 等效项?

function secondsAgo($unixtime){
   return time() - $unixtime;
}

示例用法:

echo secondsAgo(time()-10); // 10

我在Javascript中有一个unixtimestamp,需要找出从今天到那个时间戳已经过去了多少秒。

4

2 回答 2

3

JS equivelant:

function secondsAgo(unixTime) {
    return Math.round((new Date().getTime() / 1000)) - unixTime;
}
于 2013-02-04T22:08:40.577 回答
0

JS 时间戳与 Unix/PHP 时间戳相同,时间自 1970 年 1 月 1 日起,但 JS 使用毫秒代替。

var msts = new Date().getTime(); // timestamps in millisecs;
var ts = msts / 1000; // now directly compariable to PHP timestamp.

var phpts = <?php echo json_encode(time()); ?>;

var diff = phpts - ts; // difference in seconds.
于 2013-02-04T22:09:31.337 回答