Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Converting Integer to Date, reverse of strtotime

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

like image 497
Wasim A. Avatar asked Dec 16 '13 17:12

Wasim A.


People also ask

How do I convert Strtotime to date format?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

What does Strtotime return in PHP?

PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

How do you add a day in Strtotime?

echo date ( 'Y-m-d' , strtotime ( $Date . ' + 10 days' )); ?> Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds.

How do I use Strtotime?

If you want to use the PHP function strtotime to add or subtract a number of days, weeks, months or years from a date other than the current time, you can do it by passing the second optional parameter to the strtotime() function, or by adding it into the string which defines the time to parse.


2 Answers

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.

like image 116
Sabari Avatar answered Sep 16 '22 12:09

Sabari


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 
like image 37
Krish R Avatar answered Sep 18 '22 12:09

Krish R