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.

<?php
echo time()
?>

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.

<?php
echo date('m-d-y',time());
?>

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.

<?php 
// Program to demonstrate the strtotime() function 
// function when the english text is "now" 
// prints current time in second 
// here, since now means current 
echo strtotime("now"), "\n"; 
// prints the current time in date format 
echo date("Y-m-d", strtotime("now"))."\n"; 
?> 

1588244036 
2020-04-30
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.

<?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"; 
?> 

1588550400 
2020-05-04

Condition: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("9th march 2018"), "\n"; 
// prints the above time in date format 
echo date("Y-m-d", strtotime("9th march 2018"))."\n"; 
?> 

1520553600  
2018-03-09

Another strtotime() program:

  The Parse English textual datetimes into Unix timestamps

<?php
echo(strtotime("now") . "
");
echo(strtotime("13 November 2017") . "
");
echo(strtotime("+6 hours") . "
");
echo(strtotime("+2 week") . "
");
echo(strtotime("+1 week 4 days 6 hours 6 seconds") . "
");
echo(strtotime("next Tuesday") . "
");
echo(strtotime("last Saturday"));
?> 

  
1588245165
1510531200
1588266765
1589454765
1589217171
1588636800
1587772800
Important Note:
Since the time/date is not static, therefore the output may vary.