The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description. The function returns the time in seconds since the Unix Epoch. We can return the English textual date-time in date format using the date() function.
Syntax:
strtotime ($EnglishDateTime, $time_now)
Parameters: The function accepts two parameters as shown above and described below:
- $EnglishDateTime – This parameter specifies the English textual date-time description, which represents the date or time to be returned. The function parses the string and returns us the time in seconds. The parameter is mandatory
- $time_now This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.
Note: Since the time/date is not static, therefore the output will vary.
Below programs illustrate the strtotime() function.
Program 1: The below program demonstrates the strtotime()
function when the english text is “now”.
<?php // PHP program to demonstrate the strtotime() // function when the english text is "now" // prints current time in second // since now means current echo strtotime("now"), "\n"; // prints the current time in date format echo date("Y-m-d", strtotime("now"))."\n"; ?> |
Output:
1525378260 2018-05-03
Program 2: The below program demonstrates the strtotime()
function when the english text is a date.
<?php // PHP program to demonstrate the strtotime() // function when the english text is a date // prints the converted english text in second echo strtotime("12th february 2017"), "\n"; // prints the above time in date format echo date("Y-m-d", strtotime("12th february 2017"))."\n"; ?> |
Output:
1486857600 2017-02-12
Program 3: The below program demonstrates the strtotime()
function when the english text corresponds to any day.
<?php // PHP program to demonstrate the strtotime() // function when the english text corresponds to any // day // prints the converted english text in second echo strtotime("next sunday"), "\n"; // prints the above time in date format echo date("Y-m-d", strtotime("next sunday"))."\n"; ?> |
Output:
1525564800 2018-05-06
Recommended Posts:
- How to get the function name inside a function in PHP ?
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | is_numeric() Function
- PHP | Imagick adaptiveSharpenImage() Function
- PHP | XMLWriter endDtdEntity() Function
- PHP | isset() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

