PHP Function Reference

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.

Note: If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.

Note: For 32-bit versions of PHP, the valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

Note: Be aware of dates in the m/d/y or d-m-y formats: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.

❮ PHP Date and Time Reference