PHP Velho Oeste 2024

strtotime

(PHP 4, PHP 5, PHP 7, PHP 8)

strtotime将任何英文文本日期时间描述解析为 Unix 时间戳

说明

strtotime(string $datetime, ?int $baseTimestamp = null): int|false

本函数期望接受包含英文日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 UTC 起的秒数),其值跟 baseTimestamp 参数给出的时间相关,如果没有提供 baseTimestamp 参数则为当前时间。在日期和时间格式中定义了日期字符串解析,并且有几个微秒的注意事项,强烈建议查看完整详细信息。

警告

本函数返回的 Unix 时间戳不包含时区信息,为了实现对日期/时间信息进行计算,推荐使用功能更强大的 DateTimeImmutable

本函数的每个参数都使用默认时区,除非在该参数指定了时区。注意不要在每个参数中使用不同的时区,除非是故意的。参考 date_default_timezone_get() 获取定义默认时区的各种方法。

参数

datetime

日期/时间字符串。正确格式的说明详见 日期与时间格式

baseTimestamp

时间戳,用作计算相对日期的基础。

返回值

成功则返回时间戳,否则返回 false

错误/异常

在每次调用日期/时间函数时,如果时区无效则会引发 E_NOTICE 错误。参见 date_default_timezone_set()

更新日志

版本 说明
8.0.0 现在 baseTimestamp 允许为 null。

示例

示例 #1 strtotime() 例子

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

示例 #2 失败检查

<?php
$str
= 'Not Good';

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);
}
?>

注释

注意:

在这种情况下,“相对”日期还意味着,如果未提供日期/时间的特定组成部分,则将从 baseTimestamp 逐字获取。也就是说如果在 2022 年 5 月 31 日运行 strtotime('February'),将被解释为 31 February 2022,它将溢出到 3 March 的时间戳。(在闰年,应该是 2 March。)使用 strtotime('1 February') 或者 strtotime('first day of February') 可以避免这个问题。

注意:

如果指定的年份位数是两位数字,则其值 0-69 表示 2000-2069,70-99 表示 1970-1999。参阅下面的注释了解 32 位系统上可能的差异(结束日期可能是 2038-01-19 03:14:07)。

注意:

时间戳的有效范围通常从 Fri, 13 Dec 1901 20:45:54 UTC 到 Tue, 19 Jan 2038 03:14:07 UTC(这些日期对应 32 位有符号整数的最小值和最大值)。

在 64 位的 PHP 版本中,时间戳的有效范围实际上是无限的,因为 64 位在任一方向上都可以代表大约 2930 亿年。

注意:

不建议使用此函数对日期进行数学运算。推荐使用 DateTime::add()DateTime::sub() 函数。

参见

add a note add a note

User Contributed Notes 4 notes

up
10
cesarfrisa at yahoo dot es
10 months ago
This is the link to the relative formats that can be used in this function. It's not easy to find in the documentation.

https://www.php.net/manual/en/datetime.formats.relative.php

Here some examples:
<?php
$today
= date( 'Y-m-d', strtotime( 'today' ) );
echo
"Today: " . $today;

// Print: Today: 2023-06-01

//Basic Example
$tomorrow = date( 'Y-m-d', strtotime( 'tomorrow' ) );
echo
"\n\nTomorrow: " . $tomorrow;

// Print: Tomorrow: 2023-06-02

$yesterday = date( 'Y-m-d', strtotime( 'yesterday' ) );
echo
"\n\nYesterday: " . $yesterday;

// Print: Yesterday: 2023-05-31

// 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

?>
up
7
info at pipasjourney dot com
10 months ago
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
?>
up
8
Vyacheslav Belchuk
1 year ago
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
up
-5
MarkAgius at markagius dot co dot uk
11 months ago
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)<BR>\n";
print date("j/m/Y", getStrtotime("1/02/2022", "d/m/Y"))." Standard format. (d)<BR>\n";
print date("j/m/Y", getStrtotime("1/02/2022", "j/m/y"))." Standard format. (y) &lt;-- Bad date as 2022 is not two digits.<BR>\n";
print date("j/m/Y", getStrtotime("21/02/2022", "j/m/Y"))." Standard format. (j)<BR>\n";
print date("j/m/Y", getStrtotime("21/02/2022", "d/m/Y"))." Standard format. (d)<BR>\n";
print date("j/m/Y", getStrtotime("2/01/2022", "m/j/Y"))." US format.<BR>\n";
print date("j/m/Y", getStrtotime("2-01-22", "m-j-y"))." Two digit year, US format. (Y)<BR>\n";
print date("j/m/Y", getStrtotime("2-01-22", "m-j-Y"))." Two digit year, US format. (y) &lt;-- Wrong year if two digits.<BR>\n";
print date("j/m/Y", getStrtotime("3/09/1752", "j/m/Y"))." No such date.<BR>\n";
print date("j/m/Y", getStrtotime("50/00/19999", "j/m/Y"))." Bad date string.<BR>\n";
print date("j/m/Y", getStrtotime("1-02-2022", "j/m/Y"))." Wrong - or / used.<BR>\n";
[/code]
Output:
1/02/2022 Standard format. (j)
1/02/2022 Standard format. (d)
3/09/1752 Standard format. (y) <-- Bad date as 2022 is not two digits.
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) <-- Wrong year if two digits.
3/09/1752 No such date.
3/09/1752 Bad date string.
3/09/1752 Wrong - or / used.
To Top