PHP Converting Integer to Date, reverse of strtotime

Wasim A. picture Wasim A. · Dec 16, 2013 · Viewed 128.4k times · Source
<?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.

Answer

Sabari picture Sabari · Dec 16, 2013

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 strtotime in 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.

For example, strtotime("1970-01-01 00:00:00") gives you 0 and strtotime("1970-01-01 00:00:01") gives you 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:01 is 1,388,516,401 seconds after January 1 1970 00:00:00 UTC.