php PHP将整数转换为日期,与strtotime相反

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

PHP Converting Integer to Date, reverse of strtotime

phpdatetimetype-conversionstrtotime

提问by Wasim A.

<?php
echo strtotime("2014-01-01 00:00:01")."<hr>";
// output is 1388516401
?>

I am surprised if it can be reverse. I mean can I convert 1388516401 to 2014-01-01 00:00:01. What I actually want to know is, what's the logic behind this conversion. How php convert date to a specific integer.

我很惊讶它是否可以逆转。我的意思是我可以convert 1388516401 to 2014-01-01 00:00:01。我真正想知道的是,这种转换背后的逻辑是什么。php 如何将日期转换为特定的整数。

回答by Sabari

Yes you can convert it back. You can try:

是的,您可以将其转换回来。你可以试试:

date("Y-m-d H:i:s", 1388516401);

The logic behind this conversion from date to an integer is explained in strtotimein PHP:

PHP中的strtotime解释了这种从日期到整数的转换背后的逻辑:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于 now 给出的时间戳,或如果现在没有提供当前时间。

For example, strtotime("1970-01-01 00:00:00")gives you 0 and strtotime("1970-01-01 00:00:01")gives you 1.

例如,strtotime("1970-01-01 00:00:00")给你 0strtotime("1970-01-01 00:00:01")给你 1。

This means that if you are printing strtotime("2014-01-01 00:00:01")which will give you output 1388516401, so the date 2014-01-01 00:00:01is 1,388,516,401 seconds after January 1 1970 00:00:00 UTC.

这意味着如果您正在打印strtotime("2014-01-01 00:00:01")这会给您 output 1388516401,那么日期2014-01-01 00:00:01是 1970 年 1 月 1 日 00:00:00 UTC 之后的 1,388,516,401 秒。

回答by Krish R

Can you try this,

你可以试试这个吗

echo date("Y-m-d H:i:s", 1388516401);

As noted by theGame,

正如指出的theGame

This means that you pass in a string value for the time, and optionally a value for the current time, which is a UNIX timestamp. The value that is returned is an integer which is a UNIX timestamp.

这意味着您传入时间的字符串值,以及可选的当前时间值,即 UNIX 时间戳。返回的值是一个整数,它是一个 UNIX 时间戳。

echo strtotime("2014-01-01 00:00:01");

This will return into the value 1388516401, which is the UNIX timestamp for the date 2014-01-01. This can be confirmed using the date() function as like below:

这将返回值 1388516401,这是日期 2014-01-01 的 UNIX 时间戳。这可以使用 date() 函数来确认,如下所示:

echo date('Y-m-d', 1198148400); // echos 2014-01-01

回答by Vereos

I guess you are asking why is 1388516401 equal to 2014-01-01...?

我猜你在问为什么 1388516401 等于 2014-01-01 ......?

There is an historical reason for that. There is a 32-bit integer variable, called time_t, that keeps the count of the time elapsed since 1970-01-01 00:00:00. Its value expresses time in seconds. This means that in 2014-01-01 00:00:01time_twill be equal to 1388516401.

这是有历史原因的。有一个 32 位整数变量,称为time_t,它保持自1970-01-01 00:00:00以来经过的时间计数。它的值以表示时间。这意味着在2014-01-01 00:00:01time_t将等于1388516401

This leads us for sure to another interesting fact... In 2038-01-19 03:14:07time_twill reach 2147485547, the maximum value for a 32-bit number. Ever heard about John Titor and the Year 2038 problem? :D

这肯定会让我们看到另一个有趣的事实......在2038-01-19 03:14:07time_t将达到2147485547,这是 32 位数字的最大值。听说过John Titor 和 2038 年问题吗?:D

回答by SpiderLinked

The time() function displays the seconds between now and the unix epoch , 01 01 1970 (00:00:00 GMT). The strtotime() transforms a normal date format into a time() format. So the representation of that date into seconds will be : 1388516401

time() 函数显示从现在到 unix 纪元 01 01 1970 (00:00:00 GMT) 之间的秒数。strtotime() 将普通日期格式转换为 time() 格式。所以该日期到秒的表示将是:1388516401

Source: http://www.php.net/time

来源:http: //www.php.net/time