(PHP 4, PHP 5)
strtotime — 將任何英文文本的日期時間描述解析為 Unix 時間戳
說明
int strtotime ( string$time
[, int $now
= time()
] )
本函數預期接受一個包含美國英語日期格式的字符串并嘗試將其解析為
Unix 時間戳(自 January 1 1970 00:00:00 GMT 起的秒數),其值相對于
now
參數給出的時間,如果沒有提供此參數則用系統當前時間。
本函數將使用 TZ 環境變量(如果有的話)來計算時間戳。自 PHP 5.1.0 起有更容易的方法來定義時區用于所有的日期/時間函數。此過程在 date_default_timezone_get() 函數頁面中有說明。
參數
time
日期/時間字符串。正確格式的說明詳見 日期與時間格式。
now
用來計算返回值的時間戳。
返回值
成功則返回時間戳,否則返回 FALSE
。在 PHP 5.1.0
之前本函數在失敗時返回 -1。
錯誤/異常
在每 次調用日期/時間函數時,如果時區無效則會引發 E_NOTICE
錯誤,如果使用系統設定值或 TZ
環境變量,則會引發 E_STRICT
或 E_WARNING
消息。參見
date_default_timezone_set()。
更新日志
版本
說明
5.3.0
在 PHP 5.3.0 之前, 24:00 不是一個有效的格式,并且 strtotime() 會返回 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
失敗時返回 FALSE
,不再是 -1。
5.1.0
現在發布 E_STRICT
和 E_NOTICE
時區錯誤。
范例
Example #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";
?>
Example #2 失敗檢查
<?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 of F Y h:i:s A', $timestamp);
}
?>
注釋
Note:
如果給定的年份是兩位數字的格式,則其值 0-69 表示 2000-2069,70-100 表示 1970-2000。 See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
Note:
有效的時間戳通常從 Fri, 13 Dec 1901 20:45:54 GMT 到 Tue, 19 Jan 2038 03:14:07 GMT(對應于 32 位有符號整數的最小值和最大值)。不是所有的平臺都支持負的時間戳,那么日記范圍就被限制為不能早于 Unix 紀元。這意味著在 1970 年 1 月 1 日之前的日期將不能用在 Windows,一些 Linux 版本,以及幾個其它的操作系統中。不過 PHP 5.1.0 及更新的版本克服了此限制。
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.
參見
Date and Time Formats DateTime::createFromFormat() - Returns new DateTime object formatted according to the specified format checkdate() - 驗證一個格里高里日期 strptime() - 解析由 strftime 生成的日期/時間