strtotime
(PHP 4, PHP 5)
strtotime — Parse about any English textual datetime description into a Unix timestamp
Description
$time
[, int $now
= time()
] )
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 now
, or the current time if
now
is not supplied.
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
-
time
-
A date/time string. Valid formats are explained in Date and Time Formats.
-
now
-
The timestamp which is used as a base for the calculation of relative dates.
Return Values
Returns a timestamp on success, FALSE
otherwise. Previous to PHP 5.1.0,
this function would return -1 on failure.
Errors/Exceptions
Every call to a date/time function will generate a E_NOTICE
if the time zone is not valid, and/or a E_STRICT
or E_WARNING
message
if using the system settings or the TZ environment
variable. See also date_default_timezone_set()
Changelog
Version | Description |
---|---|
5.3.0 |
Prior to PHP 5.3.0, relative time formats supplied to the
time argument of strtotime()
such as this week, previous week,
last week, and next week were
interpreted to mean a 7 day period relative to the current date/time, rather
than a week period of Monday through Sunday.
|
5.3.0 |
Prior to PHP 5.3.0, 24:00 was not a valid format and
strtotime() returned FALSE .
|
5.2.7 | In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions. |
5.1.0 |
Now returns FALSE on failure, instead
of -1.
|
5.1.0 |
Now issues the |
5.0.2 | In PHP 5 up to 5.0.2, "now" and other relative times are wrongly computed from today's midnight. This differs from other versions where it is correctly computed from current time. |
5.0.0 | Microseconds began to be allowed, but they are ignored. |
4.4.0 | In PHP versions prior to 4.4.0, "next" is incorrectly computed as +2. A typical solution to this is to use "+1". |
Examples
Example #1 A strtotime() example
<?php
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";
?>
Example #2 Checking for failure
<?php
$str = 'Not Good';
// previous to PHP 5.1.0 you would compare with -1, instead of false
if (($timestamp = strtotime($str)) === false) {
echo "The string ($str) is bogus";
} else {
echo "$str == " . date('l dS \o\f F Y h:i:s A', $timestamp);
}
?>
Notes
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.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
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:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: 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.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Note:
Using this function for mathematical operations is not advisable. It is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and later, or DateTime::modify() in PHP 5.2.
See Also
- Date and Time Formats
- DateTime::createFromFormat() - Returns new DateTime object formatted according to the specified format
- checkdate() - Validate a Gregorian date
- strptime() - Parse a time/date generated with strftime
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- 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
Коментарии
Be careful when using two numbers as the year. I came across this situation:
<?php
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);
?>
Output:
1669324282
25.11.2022 00:11:22
1669237200
24.11.2022 00:00:00
Be aware of this: 1 month before the 31st day, it will return the same month:
<?php
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:
<?php
//considering today is 2023-05-31...
$firstOfThisMonth = date('Y-m') . '-01'; //returns 2023-05-01
echo date('m', strtotime($firstOfThisMonth . ' -1 month')) ; //returns 04
?>
> 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.
important - does not contain
<?php
date_default_timezone_set('Europe/Berlin');
// .... a lot of code
echo $a = strtotime('yesterday 00:00');
// in $a hour = 23:00:00 and you may not know about it
// https://onlinephp.io/c/ef696
// use DateTimeImmutable
Not sure why, but
<?php
echo strtotime("+2 hours"), "\n";
echo strtotime("+2 hrs"), "\n";
echo strtotime("+2 hourss"), "\n";
?>
are returning +2 hours, -2 hours, +8 hours. The latter two should be errors instead.