<?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.
Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );
PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With