mktime

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

mktime取得一個日期的 Unix 時間戳

說明

mktime(
    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

根據(jù)給出的參數(shù)返回 Unix 時間戳。時間戳是一個長整數(shù),包含了從 Unix 紀元(January 1 1970 00:00:00 GMT)到給定時間的秒數(shù)。

參數(shù)可以從右向左省略,任何省略的參數(shù)會被設(shè)置成本地日期和時間的當前值。

注釋

注意:

As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice: use the time() function instead.

參數(shù)

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

分鐘數(shù)。 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

秒數(shù)(一分鐘之內(nèi))。 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

月份數(shù)。 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

天數(shù)。 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

年份數(shù),可以是兩位或四位數(shù)字,0-69 對應(yīng)于 2000-2069,70-100 對應(yīng)于 1970-2000。在如今系統(tǒng)中普遍把 time_t 作為一個 32 位有符號整數(shù)的情況下,year 的合法范圍是 1901 到 2038 之間,不過此限制自 PHP 5.1.0 起已被克服了。

is_dst

本參數(shù)可以設(shè)為 1,表示正處于夏時制時間(DST),0 表示不是夏時制,或者 -1(默認值)表示不知道是否是夏時制。如果未知,PHP 會嘗試自己搞明白。這可能產(chǎn)生不可預(yù)知(但并非不正確)的結(jié)果。如果 PHP 運行的系統(tǒng)中啟用了 DST 或者 is_dst 設(shè)為 1,某些時間是無效的。例如 DST 自 2:00 生效,則所有處于 2:00 到 3:00 之間的時間都無效,mktime() 會返回一個未定義(通常為負)的值。某些系統(tǒng)(例如 Solaris 8)的 DST 在午夜生效,則 DST 生效當天的 0:30 會被計算為前一天的 23:30。

注意:

自 PHP 5.1.0 起,本參數(shù)已被廢棄。應(yīng)該使用新的時區(qū)處理特性來替代。

注意:

PHP 7.0.0 起,此參數(shù)已經(jīng)被移除。

返回值

mktime() 根據(jù)給出的參數(shù)返回 Unix 時間戳。如果參數(shù)非法,本函數(shù)返回 false(在 PHP 5.1 之前返回 -1)。

錯誤/異常

在每次調(diào)用日期/時間函數(shù)時,如果時區(qū)無效則會引發(fā) E_NOTICE 錯誤。參見 date_default_timezone_set()。

更新日志

版本 說明
7.0.0 is_dst參數(shù)已經(jīng)被移除。
5.3.0 mktime() now throws E_DEPRECATED notice if the is_dst parameter is used.
5.1.0 is_dst 參數(shù)被廢棄。出錯時函數(shù)返回 false 而不再是 -1。修正了本函數(shù)可以接受年月日參數(shù)全為零。
5.1.0 When called with no arguments, mktime() throws E_STRICT notice. Use the time() function instead.
5.1.0

現(xiàn)在發(fā)布 E_STRICTE_NOTICE 時區(qū)錯誤。

范例

示例 #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(000712000));

// Prints something like: 2006-04-05T01:02:03+00:00
echo date('c'mktime(123452006));
?>

示例 #2 mktime() 例子

mktime() 在做日期計算和驗證方面很有用,它會自動計算超出范圍的輸入的正確值。例如下面例子中每一行都會產(chǎn)生字符串 "Jan-01-1998"。

<?php
echo date("M-d-Y"mktime(00012321997));
echo 
date("M-d-Y"mktime(0001311997));
echo 
date("M-d-Y"mktime(000111998));
echo 
date("M-d-Y"mktime(0001198));
?>

示例 #3 下個月的最后一天

任何給定月份的最后一天都可以被表示為下個月的第 "0" 天,而不是 -1 天。下面兩個例子都會產(chǎn)生字符串 "The last day in Feb 2000 is: 29"。

<?php
$lastday 
mktime(000302000);
echo 
strftime("Last day in Feb 2000 is: %d"$lastday);
$lastday mktime(0004, -312000);
echo 
strftime("Last day in Feb 2000 is: %d"$lastday);
?>

注釋

警告

在 PHP 5.1.0 之前,在任何已知 Windows 版本以及一些其它系統(tǒng)下不支持負的時間戳。因此年份的有效范圍限制為 1970 到 2038。

參見

  • checkdate() - 驗證一個格里高里日期
  • gmmktime() - 取得 GMT 日期的 UNIX 時間戳
  • date() - 格式化一個本地時間/日期
  • time() - 返回當前的 Unix 時間戳