PHP strtotime() Function | What is strtotime and How to use them?

strtotime function
PHP strtotime()Function:- PHP has a date function to handle date & time to implement in web applications.
The PHP strtotime() is an inbuilt function, that used to convert an English textual date-time description to a UNIX timestamp. when we look at dates in PHP, there is a system called UNIX timestamp.

therefore, the strtotime() the function accepts a string parameter in English that represents the description of date-time. We can return the English textual date-time in date format using the date() function. the function returns the time in seconds since the UNIX Epoch.

Syntax: strtotime ($EnglishDateTime, $time_now)

Parameter Values

Parameter Description
$EnglishDateTime This parameter is required or mandatory, parameter Specifies the English textual date-time description which represents the date/time to return us the time in seconds and the function parses the string.
$time_now A parameter is optional, specifies the timestamp used as a base for the calculation of relative dates.

Technical Details

PHP Version: 4+
Return Value: It returns a timestamp on success, FALSE otherwise. this function would return -1 on failure.
PHP Changelog: PHP 5.3.0: Relative time formats such as a previous week, last week, this week and next week now interprets a week period of Monday through Sunday, rather than a 7-day period relative to the current date/time.
PHP 5.3.0: currently 24:00 is a valid format.
PHP 5.2.7: In earlier versions, if requesting a given incidence of a given weekday in an exceedingly month wherever that weekday was the first day of the month it might incorrectly add one week to the returned timestamp.
PHP 5.1.0: Returns FALSE on failure and issues E_STRICT and E_NOTICE time zone errors
PHP 5.0.2: Now properly computes “now” and other relative times from current time, not from today’s midnight
PHP 5.0.0: permits microseconds (but they’re ignored)

What is UNIX timestamp?

Therefore, these are utilized in PHP to convert an English textual date-time to a UNIX timestamp. once we examine dates in PHP, there’s a system referred to as UNIX timestamp.

furthermore, If we want to echo out function time, we will be left with a weird number that has absolutely nothing to do with the date.

Output: 1588248631

furthermore, It is UNIX timestamp in the above output and that is nothing more than a linear number that has counted the number of seconds since January 1st of 1970 but it is a useful number because we can format this being nothing more than seconds. We can use PHP to manipulate that string into what we want it to be. If I use the date function, date function requires two parameters, first are the format of date and second is time source.

Output: 04-30-20


The strtotime() Function Program:

therefore, the given below program demonstrates the strtotime() function.

Condition: When the English text is “now”. then see the output of the program.


Important Note:
Therefore, Be aware of dates in the d-m-y or m/d/y formats, If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. if the separator may be a slash (/), then the American m/d/y is assumed. To avoid potential errors, you should to YYYY-MM-DD dates or date_create_from_format() once potential.

Program Condition:When the English text corresponds to any day.



Condition:When the English text is a date.



Another strtotime() program:

  The Parse English textual datetimes into Unix timestamps


Important Note:
Since the time/date is not static, therefore the output may vary.