strtotime
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in baseTimestamp , or the current time if baseTimestamp is not supplied. The date string parsing is defined in Date and Time Formats, and has several subtle considerations. Reviewing the full details there is strongly recommended.
The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable .
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.
Parameters
A date/time string. Valid formats are explained in Date and Time Formats.
The timestamp which is used as a base for the calculation of relative dates.
Return Values
Returns a timestamp on success, false otherwise.
Errors/Exceptions
Every call to a date/time function will generate a E_WARNING if the time zone is not valid. See also date_default_timezone_set()
Changelog
Version | Description |
---|---|
8.0.0 | baseTimestamp is nullable now. |
Examples
Example #1 A strtotime() example
echo strtotime ( «now» ), «\n» ;
echo strtotime ( «10 September 2000» ), «\n» ;
echo strtotime ( «+1 day» ), «\n» ;
echo strtotime ( «+1 week» ), «\n» ;
echo strtotime ( «+1 week 2 days 4 hours 2 seconds» ), «\n» ;
echo strtotime ( «next Thursday» ), «\n» ;
echo strtotime ( «last Monday» ), «\n» ;
?>?php
Example #2 Checking for failure
if (( $timestamp = strtotime ( $str )) === false ) echo «The string ( $str ) is bogus» ;
> else echo » $str = color: #007700″>. date ( ‘l dS \o\f F Y h:i:s A’ , $timestamp );
>
?>
Notes
Note:
«Relative» date in this case also means that if a particular component of the date/time stamp is not provided, it will be taken verbatim from the baseTimestamp . That is, strtotime(‘February’) , if run on the 31st of May 2022, will be interpreted as 31 February 2022 , which will overflow into a timestamp on 3 March . (In a leap year, it would be 2 March .) Using strtotime(‘1 February’) or strtotime(‘first day of February’) would avoid that problem.
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. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
Note:
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:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() .
See Also
- DateTimeImmutable
- DateTimeImmutable::createFromFormat() — Parses a time string according to a specified format
- Date and Time Formats
- checkdate() — Validate a Gregorian date
- strptime() — Parse a time/date generated with strftime
User Contributed Notes 4 notes
Be aware of this: 1 month before the 31st day, it will return the same month:
echo date ( ‘m’ , strtotime ( ‘2023-05-30 -1 month’ )) ; //returns 04
echo date ( ‘m’ , strtotime ( ‘2023-05-31 -1 month’ )) ; //returns 05, not 04
?>
So, don’t use this to operate on the month of the result.
A better way to know what month was the previous month would be:
//considering today is 2023-05-31.
$firstOfThisMonth = date ( ‘Y-m’ ) . ‘-01’ ; //returns 2023-05-01
echo date ( ‘m’ , strtotime ( $firstOfThisMonth . ‘ -1 month’ )) ; //returns 04
?>
Be careful when using two numbers as the year. I came across this situation:
echo strtotime ( ‘24.11.22’ );
echo date ( ‘d.m.Y H:i:s’ , 1669324282 ) . «\n\n» ;
// But
echo strtotime ( ‘24.11.2022’ );
echo date ( ‘d.m.Y H:i:s’ , 1669237200 );
This is the link to the relative formats that can be used in this function. It’s not easy to find in the documentation.
Here some examples:
$today = date ( ‘Y-m-d’ , strtotime ( ‘today’ ) );
echo «Today: » . $today ;
//Basic Example
$tomorrow = date ( ‘Y-m-d’ , strtotime ( ‘tomorrow’ ) );
echo «\n\nTomorrow: » . $tomorrow ;
$yesterday = date ( ‘Y-m-d’ , strtotime ( ‘yesterday’ ) );
echo «\n\nYesterday: » . $yesterday ;
// Add o subtract Month
$variable = date ( ‘Y-m-d’ , strtotime ( ‘+1 month’ ) );
echo «\n\nAdd one month: » . $variable ;
// Print: Add one month: 2023-07-01
$variable = date ( ‘Y-m-d’ , strtotime ( ‘-1 month’ ) );
echo «\n\nSubstract one month: » . $variable ;
// Print: Substract one month: 2023-05-01
// More complex formats:
// ERROR / BUG: If today is the last day of the month. This examples can be one error or bug.
// If you are looking el first day of previous or next month you dont must use last Relative Format.
// You must use ‘first day of 2 months ago’ or ‘first day of previous month’
// See you:
// https://bugs.php.net/bug.php?id=22486
// https://bugs.php.net/bug.php?id=44073
// strtotime(«+1 Month»)
// produces the wrong result on the 31st day of the month.
// on January 31, date(«m»,strtotime(«+1 Month»)) will return 03 instead of 02
// Another example:
$dt1 = strtotime ( «2023-01-30» );
$month = strtotime ( «+1 month» , $dt1 );
echo «\n\nToday is: » . date ( ‘Y-m-d’ , $dt1 );
echo «\nBUG add month: » . date ( «Y-m-d» , $month );
// Print:
// Today is: 2023-01-30
// BUG add month: 2023-03-02
$dt1 = strtotime ( «2023-02-28» );
$month = strtotime ( «-1 month» , $dt1 );
echo «\n\nToday is: » . date ( ‘Y-m-d’ , $dt1 );
echo «\nBUG substract month: » . date ( «Y-m-d» , $month );
// Print:
// Today is: 2023-02-28
// BUG substract month: 2023-01-28
$dt1 = strtotime ( «2023-01-30» );
$month = strtotime ( «first day of next month» , $dt1 );
echo «\n\nToday is: » . date ( ‘Y-m-d’ , $dt1 );
echo «\nFirst day of next month: » . date ( «Y-m-d» , $month );
// Print:
// Today is: 2023-01-30
// First day of next month: 2023-02-01
$dt1 = strtotime ( «2023-02-28» );
$month = strtotime ( «last day of last month» , $dt1 );
echo «\n\nToday is: » . date ( ‘Y-m-d’ , $dt1 );
echo «\nLast day of last month: » . date ( «Y-m-d» , $month );
// Print:
// Today is: 2023-02-28
// Last day of last month: 2023-01-31
$dt1 = strtotime ( «2023-02-28» );
$month = strtotime ( «first day of 2 months ago» , $dt1 );
echo «\n\nToday is: » . date ( ‘Y-m-d’ , $dt1 );
echo «\nFirst day of 2 months ago: » . date ( «Y-m-d» , $month );
// Print:
// Today is: 2023-02-28
// First day of 2 months ago: 2022-12-01
Note:
If day of month is 12 or less, or year is entered as a two digit number and less than 31 or 12 then you may get the wrong time value.
If you know the format used with the date string, then use the following code: (PHP version 5.5 or later)
[code]function getStrtotime($timeDateStr, $formatOfStr=»j/m/Y») // Same as strtotime() but using the format $formatOfStr.
// Works with PHP version 5.5 and later.
// On error reading the time string, returns a date that never existed. 3/09/1752 Julian/Gregorian calendar switch.
$timeStamp = DateTimeImmutable::createFromFormat($formatOfStr,$timeDateStr);
if($timeStamp===false) // Bad date string or format string.
return -6858133619; // 3/09/1752
> else // Date string and format ok.
return $timeStamp->format(«U»); // UNIX timestamp from 1/01/1970, 0:00:00 gmt
>
>
print date(«j/m/Y», getStrtotime(«1/02/2022», «j/m/Y»)).» Standard format. (j)\n»;
print date(«j/m/Y», getStrtotime(«1/02/2022», «d/m/Y»)).» Standard format. (d)\n»;
print date(«j/m/Y», getStrtotime(«1/02/2022», «j/m/y»)).» Standard format. (y) <— Bad date as 2022 is not two digits.\n»;
print date(«j/m/Y», getStrtotime(«21/02/2022», «j/m/Y»)).» Standard format. (j)\n»;
print date(«j/m/Y», getStrtotime(«21/02/2022», «d/m/Y»)).» Standard format. (d)\n»;
print date(«j/m/Y», getStrtotime(«2/01/2022», «m/j/Y»)).» US format.\n»;
print date(«j/m/Y», getStrtotime(«2-01-22», «m-j-y»)).» Two digit year, US format. (Y)\n»;
print date(«j/m/Y», getStrtotime(«2-01-22», «m-j-Y»)).» Two digit year, US format. (y) <— Wrong year if two digits.\n»;
print date(«j/m/Y», getStrtotime(«3/09/1752», «j/m/Y»)).» No such date.\n»;
print date(«j/m/Y», getStrtotime(«50/00/19999», «j/m/Y»)).» Bad date string.\n»;
print date(«j/m/Y», getStrtotime(«1-02-2022», «j/m/Y»)).» Wrong — or / used.\n»;
[/code]Output:
1/02/2022 Standard format. (j)
1/02/2022 Standard format. (d)
3/09/1752 Standard format. (y) 21/02/2022 Standard format. (j)
21/02/2022 Standard format. (d)
1/02/2022 US format.
1/02/2022 Two digit year, US format. (Y)
1/02/0022 Two digit year, US format. (y) 3/09/1752 No such date.
3/09/1752 Bad date string.
3/09/1752 Wrong — or / used.
- Date/Time Functions
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get