PHP strtotime() Function
The PHP strtotime() function converts any English textual datetime description into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC).
The function attempts to parse the datetime string into a Unix timestamp, relative to the timestamp given in baseTimestamp, or the current time if baseTimestamp is not provided.
Syntax
strtotime(datetime, baseTimestamp)
Parameters
datetime |
Required. Specify a date/time string. It should be in valid Date and Time Formats. |
baseTimestamp |
Optional. Specify the timestamp which is used as a base for the calculation of relative dates. |
Return Value
Returns a timestamp on success, false otherwise.
Exceptions
If the time zone is not valid, generates a E_WARNING.
Example: strtotime() example
The example below shows the usage of strtotime() function.
<?php //current time as Unix timestamp and //formatting it using date() function echo strtotime("now")."\n"; echo date("d-M-Y", strtotime("now"))."\n"; //1 week ahead of current time echo "\n".strtotime("+1 week")."\n"; echo date("d-M-Y", strtotime("+1 week"))."\n"; //1 week 2 days 5 hours ahead of current time echo "\n".strtotime("+1 week 2 days 5 hours")."\n"; echo date("d-M-Y", strtotime("+1 week 2 days 5 hours"))."\n"; ?>
The output of the above code will be:
1632930974 29-Sep-2021 1633535774 06-Oct-2021 1633726574 08-Oct-2021
Example: another datetime descriptions
Consider the example below where some more datetime description is used.
<?php //next monday echo "\n".strtotime("next Monday")."\n"; echo date("d-M-Y", strtotime("next Monday"))."\n"; //given date as Unix timestamp and //formatting it using date() function echo "\n".strtotime("15th December 2015")."\n"; echo date("d-M-Y", strtotime("15th December 2015"))."\n"; //1 week past 15th October 2014 echo "\n".strtotime("+1 week", 1413331200)."\n"; echo date("d-M-Y", strtotime("+1 week", 1413331200))."\n"; ?>
The output of the above code will be:
1633305600 04-Oct-2021 1450137600 15-Dec-2015 1413936000 22-Oct-2014
Example: Checking for valid datetime
The function returns false on failure which can be used to check whether a given datetime is valid or not. Consider the example below:
<?php $str = 'Good Morning'; if (strtotime($str) === false) echo "The string ($str) is not a valid date/time."; else echo "$str == " . date("d-M-Y h:i:s", strtotime($str)); ?>
The output of the above code will be:
The string (Good Morning) is not a valid date/time.
❮ PHP Date and Time Reference