(PHP 4, PHP 5, PHP 7, PHP 8)
mktime — 取得一個(gè)日期的 Unix 時(shí)間戳
$hour
= date("H"),$minute
= date("i"),$second
= date("s"),$month
= date("n"),$day
= date("j"),$year
= date("Y"),$is_dst
= -1根據給出的參數返回 Unix 時(shí)間戳。時(shí)間戳是一個(gè)長(cháng)整數,包含了從 Unix 紀元(January 1 1970 00:00:00 GMT)到給定時(shí)間的秒數。
參數可以從右向左省略,任何省略的參數會(huì )被設置成本地日期和時(shí)間的當前值。
注意:
As of PHP 5.1, when called with no arguments, mktime() throws an
E_STRICT
notice: use the time() function instead.
hour
小時(shí)數。
The number of the hour relative to the start of the day determined by
month
, day
and year
.
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 作為一個(gè) 32
位有符號整數的情況下,year
的合法范圍是 1901 到 2038 之間,不過(guò)此限制自
PHP 5.1.0 起已被克服了。
is_dst
本參數可以設為 1,表示正處于夏時(shí)制時(shí)間(DST),0
表示不是夏時(shí)制,或者 -1(默認值)表示不知道是否是夏時(shí)制。如果未知,PHP
會(huì )嘗試自己搞明白。這可能產(chǎn)生不可預知(但并非不正確)的結果。如果
PHP 運行的系統中啟用了 DST 或者 is_dst
設為 1,某些時(shí)間是無(wú)效的。例如 DST 自 2:00 生效,則所有處于
2:00 到 3:00 之間的時(shí)間都無(wú)效,mktime()
會(huì )返回一個(gè)未定義(通常為負)的值。某些系統(例如
Solaris 8)的 DST 在午夜生效,則 DST 生效當天的 0:30
會(huì )被計算為前一天的 23:30。
注意:
自 PHP 5.1.0 起,本參數已被廢棄。應該使用新的時(shí)區處理特性來(lái)替代。
注意:
PHP 7.0.0 起,此參數已經(jīng)被移除。
mktime() 根據給出的參數返回 Unix
時(shí)間戳。如果參數非法,本函數返回
false
(在 PHP 5.1 之前返回 -1
)。
在每次調用日期/時(shí)間函數時(shí),如果時(shí)區無(wú)效則會(huì )引發(fā) E_NOTICE
錯誤。參見(jiàn)
date_default_timezone_set()。
版本 | 說(shuō)明 |
---|---|
7.0.0 |
is_dst 參數已經(jīng)被移除。
|
5.3.0 |
mktime() now throws E_DEPRECATED notice
if the is_dst parameter is used.
|
5.1.0 |
is_dst 參數被廢棄。出錯時(shí)函數返回
false 而不再是 -1 。修正了本函數可以接受年月日參數全為零。
|
5.1.0 |
When called with no arguments, mktime() throws
E_STRICT notice. Use the
time() function instead.
|
5.1.0 |
現在發(fā)布 |
示例 #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() 在做日期計算和驗證方面很有用,它會(huì )自動(dòng)計算超出范圍的輸入的正確值。例如下面例子中每一行都會(huì )產(chǎn)生字符串 "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 下個(gè)月的最后一天
任何給定月份的最后一天都可以被表示為下個(gè)月的第 "0" 天,而不是 -1 天。下面兩個(gè)例子都會(huì )產(chǎn)生字符串 "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 版本以及一些其它系統下不支持負的時(shí)間戳。因此年份的有效范圍限制為 1970 到 2038。