PHP将整数转换为日期,与strtotime相反 [英] PHP Converting Integer to Date, reverse of strtotime

查看:372
本文介绍了PHP将整数转换为日期,与strtotime相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

推荐答案

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

Yes you can convert it back. You can try:

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

从日期到整数的这种转换背后的逻辑在 strotime

The logic behind this conversion from date to an integer is explained in strotime in php.net.

It is the number of seconds since January 1 1970 00:00:00 UTC.  

Printing echo strtotime("1970-01-01 00:00:00") gives you 0.
Printing echo strtotime("1970-01-01 00:00:01") gives you 1.

这意味着如果你打印 strtotime(2014-01- 01 00:00:01),将输出 1388516401 ,所以日期 2014-01-01 00:00:01 1388516401 从1970年1月1日00:00:00 UTC。

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:01 is 1388516401 seconds from January 1 1970 00:00:00 UTC.

希望这有助于您

这篇关于PHP将整数转换为日期,与strtotime相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆