40
<?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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Wasim A.
  • 9,660
  • 22
  • 90
  • 120
  • 9
    `$time = 1388516401; echo date("Y-m-d H:i:s", $time);` I'd like to refer you to the [date](http://www.php.net/manual/en/function.date.php) documentation. – Chris Forrence Dec 16 '13 at 17:38
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Wasim A. Nov 23 '14 at 15:31
  • 1
    hmmm, good question title, but the question itself is useless. – Andrew Feb 02 '16 at 20:01

4 Answers4

73

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.

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Sabari
  • 6,205
  • 1
  • 27
  • 36
  • I have this integer `-243219600` which is relative to **18/04/1962** when I convert it `date('d/m/Y', $r)` it gives me the date **17/04/1962** – Alaeddine Sep 10 '15 at 10:24
  • strtotime("1970-01-01 00:00:00") only gives you 0 if your date_default_timezone is set to UTC -- both parameters from strtotime take the default timezone into its calculation – Jordan Feb 03 '23 at 22:23
13

Can you try this,

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

As noted by 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.

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:

echo date('Y-m-d', 1198148400); // echos 2014-01-01
Community
  • 1
  • 1
Krish R
  • 22,583
  • 7
  • 50
  • 59
4

I guess you are asking why is 1388516401 equal to 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:01 time_t will be equal to 1388516401.

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

Vereos
  • 503
  • 4
  • 15
0

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

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

SpiderLinked
  • 363
  • 1
  • 6
  • 14