PHP strtotime 和 JavaScript Date.parse 返回不同的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14868066/
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-24 18:11:47  来源:igfitidea点击:

PHP strtotime and JavaScript Date.parse return different timestamps

phpjavascriptdatedatetimetimestamp

提问by Puzzled Boy

I am getting same date time seconds value in JavaScript which is given by strtotime()in PHP. But i need same value in JavaScript.

我在 JavaScript 中获得了相同的日期时间秒值,该值由strtotime()PHP给出。但我需要在 JavaScript 中使用相同的值。

PHP Code

PHP代码

echo strtotime("2011-01-26 13:51:50");
// 1296046310

JavaScript Code

JavaScript 代码

var d = Date.parse("2011-01-26 13:51:50");
console.log(d);
// 1296030110000

回答by Ja?ck

You need to use the same time-zone for a sane comparison:

您需要使用相同的时区进行合理的比较:

echo strtotime("2011-01-26 13:51:50 GMT");
// 1296049910

var d = Date.parse("2011-01-26 13:51:50 GMT") / 1000;
console.log(d);
// 1296049910

Update

更新

According to the standard, only RFC 2822 formatted dates are well supported:

根据标准,只有 RFC 2822 格式的日期得到很好的支持:

Date.parse("Wed, 26 Jan 2011 13:51:50 +0000") / 1000

To generate such a date, you can use gmdate('r')in PHP:

要生成这样的日期,您可以gmdate('r')在 PHP 中使用:

echo gmdate('r', 1296049910);

回答by Dipesh Parmar

JavaScript uses milliseconds as a timestamp, whereas PHP uses seconds. As a result, you get very different dates, as it is off by a factor 1000.

JavaScript 使用毫秒作为时间戳,而 PHP 使用秒。因此,您会得到非常不同的日期,因为它相差 1000 倍。

sample

样本

echo date('Y-m-d', TIMESTAMP / 1000);

echo date('Y-m-d', TIMESTAMP / 1000);

Comment Response

评论回复

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    function toTimestamp(year,month,day,hour,minute,second)
    {
        var datum = new Date(Date.UTC(year,month-1,day,hour,minute,second));
        return datum.getTime()/1000;
    }

    $(function()
    {
        console.log(toTimestamp(2011,01,26,13,51,50));

    });

</script>

<?php

echo $the_date = strtotime("2011-01-26 13:51:50");

回答by Salman A

strtotime()and Date.parse()yield UNIX timestamps with a resolution of seconds and milliseconds respectively. However, if timezone information is missing from the input string, local time is assumed. So the input string 2011-01-26T13:51:50may produce different output on different machines even if PHP (or JavaScript) is used to generate the timestamps on both machines.

strtotime()Date.parse()产生分辨率分别为秒和毫秒的 UNIX 时间戳。但是,如果输入字符串中缺少时区信息,则假定为本地时间。因此,2011-01-26T13:51:50即使使用 PHP(或 JavaScript)在两台机器上生成时间戳,输入字符串也可能在不同机器上产生不同的输出。

The solution is to explicitly specify the timezone in the strings. This should produce the same result on any machine:

解决方案是在字符串中明确指定时区。这应该在任何机器上产生相同的结果:

Date.parse("Jan 26, 2011 13:51:50 GMT+0500") / 1000; // 1296031910
strtotime("Jan 26, 2011 13:51:50 GMT+0500");         // 1296031910