PHP Converting Integer to Date, reverse of strtotime
Asked Answered
F

4

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

Fissiparous answered 16/12, 2013 at 17:36 Comment(3)
$time = 1388516401; echo date("Y-m-d H:i:s", $time); I'd like to refer you to the date documentation.Randarandal
possible duplicate of Convert one date format into another in PHPFissiparous
hmmm, good question title, but the question itself is useless.Publicly
I
76

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.

Incidental answered 16/12, 2013 at 17:46 Comment(2)
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/1962Kalila
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 calculationEparch
O
16

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
Outroar answered 16/12, 2013 at 17:38 Comment(0)
D
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

Dowzall answered 16/12, 2013 at 17:49 Comment(0)
C
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

Chimkent answered 16/12, 2013 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.