mktime
(PHP 4, PHP 5, PHP 7, PHP 8)
mktime — 取得一個日期的 Unix 時間戳
說明
int
$hour
= date("H"),int
$minute
= date("i"),int
$second
= date("s"),int
$month
= date("n"),int
$day
= date("j"),int
$year
= date("Y"),int
$is_dst
= -1): int
根據給出的參數返回 Unix 時間戳。時間戳是一個長整數,包含了從 Unix 紀元(January 1 1970 00:00:00 GMT)到給定時間的秒數。
參數可以從右向左省略,任何省略的參數會被設定成本地日期和時間的當前值。
註釋
注意:
As of PHP 5.1, when called with no arguments, mktime() throws an
E_STRICT
notice: use the time() function instead.
參數
-
hour
-
小時數。 The number of the hour relative to the start of the day determined by
month
,day
andyear
. Negative values reference the hour before midnight of the day in question. Values greater than 23 reference the appropriate hour in the following day(s). -
minute
-
分鐘數。 The number of the minute relative to the start of the
hour
. Negative values reference the minute in the previous hour. Values greater than 59 reference the appropriate minute in the following hour(s). -
second
-
秒數(一分鐘之內)。 The number of seconds relative to the start of the
minute
. Negative values reference the second in the previous minute. Values greater than 59 reference the appropriate second in the following minute(s). -
month
-
月份數。 The number of the month relative to the end of the previous year. Values 1 to 12 reference the normal calendar months of the year in question. Values less than 1 (including negative values) reference the months in the previous year in reverse order, so 0 is December, -1 is November, etc. Values greater than 12 reference the appropriate month in the following year(s).
-
day
-
天數。 The number of the day relative to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
-
year
-
年份數,可以是兩位或四位數字,0-69 對應于 2000-2069,70-100 對應于 1970-2000。在如今系統中普遍把 time_t 作為一個 32 位有符號整數的情況下,
year
的合法範圍是 1901 到 2038 之間,不過此限制自 PHP 5.1.0 起已被克服了。 -
is_dst
-
本參數可以設為 1,表示正處於夏時制時間(DST),0 表示不是夏時制,或者 -1(預設值)表示不知道是否是夏時制。如果未知,PHP 會嘗試自己搞明白。這可能產生不可預知(但並非不正確)的結果。如果 PHP 執行的系統中啟用了 DST 或者
is_dst
設為 1,某些時間是無效的。例如 DST 自 2:00 生效,則所有處於 2:00 到 3:00 之間的時間都無效,mktime() 會返回一個未定義(通常為負)的值。某些系統(例如 Solaris 8)的 DST 在午夜生效,則 DST 生效當天的 0:30 會被計算為前一天的 23:30。注意:
自 PHP 5.1.0 起,本參數已被廢棄。應該使用新的時區處理特性來替代。
注意:
PHP 7.0.0 起,此參數已經被移除。
返回值
mktime() 根據給出的參數返回 Unix
時間戳。如果參數非法,本函式返回
false
(在 PHP 5.1 之前返回 -1
)。
錯誤/異常
在每次呼叫日期/時間函式時,如果時區無效則會引發 E_NOTICE
錯誤。參見
date_default_timezone_set()。
更新日誌
版本 | 說明 |
---|---|
7.0.0 |
is_dst 參數已經被移除。
|
5.3.0 |
mktime() now throws E_DEPRECATED notice
if the is_dst parameter is used.
|
5.1.0 |
is_dst 參數被廢棄。出錯時函式返回
false 而不再是 -1 。修正了本函式可以接受年月日參數全為零。
|
5.1.0 |
When called with no arguments, mktime() throws
E_STRICT notice. Use the
time() function instead.
|
5.1.0 |
現在發佈 |
範例
示例 #1 基本例子
<?php
// Set the default timezone to use. Available as of PHP 5.1
date_default_timezone_set('UTC');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
// Prints something like: 2006-04-05T01:02:03+00:00
echo date('c', mktime(1, 2, 3, 4, 5, 2006));
?>
示例 #2 mktime() 例子
mktime() 在做日期計算和驗證方面很有用,它會自動計算超出範圍的輸入的正確值。例如下面例子中每一行都會產生字串 "Jan-01-1998"。
<?php
echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 13, 1, 1997));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 1998));
echo date("M-d-Y", mktime(0, 0, 0, 1, 1, 98));
?>
示例 #3 下個月的最後一天
任何給定月份的最後一天都可以被表示為下個月的第 "0" 天,而不是 -1 天。下面兩個例子都會產生字串 "The last day in Feb 2000 is: 29"。
<?php
$lastday = mktime(0, 0, 0, 3, 0, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
?>
註釋
在 PHP 5.1.0 之前,在任何已知 Windows 版本以及一些其它系統下不支援負的時間戳。因此年份的有效範圍限制為 1970 到 2038。
參見
- checkdate() - 驗證一個格里高里日期
- gmmktime() - 取得 GMT 日期的 UNIX 時間戳
- date() - 格式化一個本地時間/日期
- time() - 返回目前的 Unix 時間戳