PHP strtotime() Function
Definition and Usage
The strtotime() function accepts a date/time string, with textual date/time values and parses it as an Unix timestamp.
Syntax
strtotime($time)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
time(Mandatory) This value represents the date/time string. |
| 2 |
now(Optional) This represents a timestamp which is used as a base for the calculation of relative dates.. |
Return Values
PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.
PHP Version
This function was first introduced in PHP Version 4.0 and, works with all the later versions.
Example
Following example demonstrates the usage of the strtotime() function −
Live Demo
<?php
$str = strtotime("12 September 2009");
print("Timestamp: ".$str);
?>
This will produce following result −
Timestamp: 1252713600
Example
If you pass "now" as a parameter this function returns the current time stamp
Live Demo
<?php
$str = strtotime("now");
print("Timestamp: ".$str);
?>
This will produce following result −
Timestamp: 1589369948
Example
Now letus invoke this method by passing various date values −
Live Demo
<?php
print("Time stamp of 25 December 2019: ".strtotime("25 December 2019")."\n");
print("Time stamp of +26 day: ".strtotime("+26 day")."\n");
print("Time stamp of +3 week: ".strtotime("+3 week")."\n");
print("Time stamp of +3 month 9 days 9 hours: ".strtotime("+3 month 9 days 9 hours")."\n");
print("Time stamp of last Sunday: ".strtotime("last Sunday")."\n");
print("Time stamp of next Friday: ".strtotime("next Friday")."\n");
?>
This will produce the following output −
Time stamp of 25 December 2019: 1577232000 Time stamp of +26 day: 1591617718 Time stamp of +3 week: 1591185718 Time stamp of +3 month 9 days 9 hours: 1598130118 Time stamp of last Sunday: 1589068800 Time stamp of next Friday: 1589500800
Example
Live Demo<?php $timestamp = strtotime( "February 15, 2015" ); print date( 'Y-m-d', $timestamp ); ?>
This will produce the following output −
2015-02-15