Javascript 和 PHP 之间的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15593759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:31:58  来源:igfitidea点击:

Timestamp between Javascript and PHP

phpjavascripttimestamp

提问by indapublic

Javascript:

Javascript:

I have object cell with something date

我有一些日期的对象单元格

params.date = cell.getDate();
params.timestamp = cell.getDate().getTime() / 1000;
console.log(params);

Object {date: Thu May 09 2013 00:00:00 GMT+0800 (China Standard Time), timestamp: 1368028800}

对象 {日期:2013 年 5 月 9 日星期四 00:00:00 GMT+0800(china标准时间),时间戳:1368028800}

Then I try to check timestamp in PHP

然后我尝试在 PHP 中检查时间戳

$date = '1368028800';
echo date('Y-m-d', $date);

2013-05-08

2013-05-08

Difference in one day. Why?

一日之差。为什么?

采纳答案by symcbean

params.date = cell.getDate();

Returns the DATE not the TIME.

返回日期而不是时间。

params.timestamp = cell.getDate().getTime() / 1000;

is converting the date into a date+time - not reading the current time.

正在将日期转换为日期+时间 - 不读取当前时间。

But even if you get the timestamp in javascript, the output of PHP's date function will depend on what timezone it is in.

但即使您在 javascript 中获得时间戳,PHP 日期函数的输出也将取决于它所在的时区。

回答by muni

When you get timestamp from Javacript date object :

当您从 Javacript 日期对象获取时间戳时:

it output will be interms of milli-seconds

它的输出将以毫秒为单位

 <script>
        var d = new Date();
        alert(d.getTime());

     </script>

output : 1386746353000

输出:1386746353000

Where as php date object timestamp interms of seconds

以秒为单位的 php 日期对象时间戳在哪里

<?php 
        $date = new DateTime();
        echo $current_timestamp = $date->getTimestamp();
     ?>

output : 1386746353

输出:1386746353

So when you are going to use javascript date object timestamp with php date object you should divide timestamp of javascript by 1000 and use it in php

因此,当您打算将 javascript 日期对象时间戳与 php 日期对象一起使用时,您应该将 javascript 的时间戳除以 1000 并在 php 中使用它

回答by user819236

The UNIX Timestamp 1368028800 corresponds to the date Wed, 08 May 2013 16:00:00 GMT. In your example, you are on the China Standard Time (GMT+8), so Javascript display the datetime for this timezone (8 hours later -> 09 May)

UNIX 时间戳 1368028800 对应于格林威治标准时间 2013 年 5 月 8 日星期三 16:00:00 的日期。在您的示例中,您使用的是china标准时间 (GMT+8),因此 Javascript 显示该时区的日期时间(8 小时后 -> 5 月 9 日)