PHP 将整数转换为日期,strtotime 的反转

标签 php date time type-conversion strtotime

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

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

最佳答案

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

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

从日期到整数的转换背后的逻辑在 strtotime 中有解释。在 PHP 中:

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.

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

这意味着,如果您正在打印 strtotime("2014-01-01 00:00:01"),这将为您提供输出 1388516401,因此日期 2014-01-01 00:00:01 是 1970 年 1 月 1 日 00:00:00 UTC 之后的 1,388,516,401 秒。

关于PHP 将整数转换为日期,strtotime 的反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617100/

相关文章:

php - 良好的图像调整框架

java - Joda-时间周期计算

python - 制作一个简单的《太空侵略者》克隆——pygame.time.delay 导致崩溃

c++ - for循环中c++中的时间(以毫秒为单位)始终相同

linux - 早于 2.6.28 的 linux 版本中的 clock_monotonic_raw 替代方案

php - 找不到此 jQuery 代码中的错误

php - 缓慢的 PHP session

php - 用php上传数据到mysql

javascript - 将 JavaScript 日期格式设置为 yyyy-mm-dd

php - 如果要在多个时区运行应用程序,我们应该以日期格式还是以日期时间格式存储日期?