mktime 取得一个日期的 Unix 时间戳
发表日期:2021-07-01 08:55:28 | 来源: | | 浏览(1370) 分类:Date/Time 函数
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_STRICTnotice: use the time() function instead.
参数
-
hour -
小时数。 The number of the hour relative to the start of the day determined by
month,dayandyear. 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 错误,如果使用系统设定值或 TZ
环境变量,则会引发 E_STRICT 或 E_WARNING 消息。参见
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.1date_default_timezone_set('UTC');
// Prints: July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
// Prints something like: 2006-04-05T01:02:03+00:00echo 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 时间戳
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- checkdate 验证一个格里高里日期(0)
- date_add 别名 DateTime::add()(0)
- date_create_from_format 别名 DateTime::createFromFormat()(0)
- date_create_immutable_from_format 别名 DateTimeImmutable::createFromFormat()(0)
- date_create_immutable 别名 DateTimeImmutable::__construct()(0)
- date_create 别名 DateTime::__construct()(0)
- date_date_set 别名 DateTime::setDate()(0)
- date_default_timezone_get 取得一个脚本中所有日期时间函数所使用的默认时区(0)
- date_default_timezone_set 设定用于一个脚本中所有日期时间函数的默认时区(0)
- date_diff 别名 DateTime::diff()(0)
- date_format 别名 DateTime::format()(0)
- date_get_last_errors 别名 DateTime::getLastErrors()(0)
- date_interval_create_from_date_string 别名 DateInterval::createFromDateString()(0)
- date_interval_format 别名 DateInterval::format()(0)
- date_isodate_set 别名 DateTime::setISODate()(0)
- date_modify 别名 DateTime::modify()(0)
- date_offset_get 别名 DateTime::getOffset()(0)
- date_parse_from_format Get info about given date formatted according to the specified format(0)
- date_parse 返回指定日期/时间的详细信息的关联数组(0)
- date_sub 别名 DateTime::sub()(0)
- date_sun_info Returns an array with information about sunset/sunrise and twilight begin/end(0)
- date_sunrise 返回给定的日期与地点的日出时间(0)
- date_sunset 返回给定的日期与地点的日落时间(0)
- date_time_set 别名 DateTime::setTime()(0)
- date_timestamp_get 别名 DateTime::getTimestamp()(0)
- date_timestamp_set 别名 DateTime::setTimestamp()(0)
- date_timezone_get 别名 DateTime::getTimezone()(0)
- date_timezone_set 别名 DateTime::setTimezone()(0)
- date 格式化一个本地时间/日期(0)
- getdate 取得日期/时间信息(0)
- gettimeofday 取得当前时间(0)
- gmdate 格式化一个 GMT/UTC 日期/时间(0)
- gmmktime 取得 GMT 日期的 UNIX 时间戳(0)
- gmstrftime 根据区域设置格式化 GMT/UTC 时间/日期(0)
- idate 将本地时间日期格式化为整数(0)
- localtime 取得本地时间(0)
- microtime 返回当前 Unix 时间戳和微秒数(0)
- mktime 取得一个日期的 Unix 时间戳(0)
- strftime 根据区域设置格式化本地时间/日期(0)
- strptime 解析由 strftime() 生成的日期/时间(0)
- strtotime 将任何字符串的日期时间描述解析为 Unix 时间戳(0)
- time 返回当前的 Unix 时间戳(0)
- timezone_abbreviations_list 别名 DateTimeZone::listAbbreviations()(0)
- timezone_identifiers_list 别名 DateTimeZone::listIdentifiers()(0)
- timezone_location_get 别名 DateTimeZone::getLocation()(0)
- timezone_name_from_abbr Returns the timezone name from abbreviation(0)
- timezone_name_get 别名 DateTimeZone::getName()(0)
- timezone_offset_get 别名 DateTimeZone::getOffset()(0)
- timezone_open 别名 DateTimeZone::__construct()(0)
- timezone_transitions_get 别名 DateTimeZone::getTransitions()(0)
- timezone_version_get 获取 timezonedb 的版本(0)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号