mktime() - php 日期时间函数
mktime()
(PHP 4, PHP 5, PHP 7)
取得一个日期的 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根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从 Unix 纪元(January 1 1970 00:00:00 GMT)到给定时间的秒数。
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
注释
Note:As of PHP 5.1, when called with no arguments,mktime()throws anE_STRICT
notice: use thetime()function instead.
参数
$hour小时数。 The number of the hour relative to the start of the day determined by$month,$dayand$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 作为一个 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。
Note:自 PHP 5.1.0 起,本参数已被废弃。应该使用新的时区处理特性来替代。Note:
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 throwsE_DEPRECATED notice if the$is_dstparameter is used. |
5.1.0 | $is_dst参数被废弃。出错时函数返回FALSE 而不再是-1。修正了本函数可以接受年月日参数全为零。 |
5.1.0 | When called with no arguments,mktime()throwsE_STRICT notice. Use thetime()function instead. |
5.1.0 | 现在发布 |
范例
基本例子
Example #2mktime()例子
mktime()在做日期计算和验证方面很有用,它会自动计算超出范围的输入的正确值。例如下面例子中每一行都会产生字符串"Jan-01-1998"。
下个月的最后一天
任何给定月份的最后一天都可以被表示为下个月的第"0"天,而不是-1 天。下面两个例子都会产生字符串"The last day in Feb 2000 is: 29"。
注释
Caution在 PHP 5.1.0 之前,在任何已知 Windows 版本以及一些其它系统下不支持负的时间戳。因此年份的有效范围限制为 1970 到 2038。
参见
checkdate()
验证一个格里高里日期gmmktime()
取得 GMT 日期的 UNIX 时间戳date()
格式化一个本地时间/日期time()
返回当前的 Unix 时间戳
Do remember that, counter-intuitively enough, the arguments for month and day are inversed (or middle-endian). A common mistake for Europeans seems to be to feed the date arguments in the expected order (big endian or little endian). It's clear to see where this weird order comes from (even with the date being big endian the order for all arguments would still be mixed - it's obviously based on the American date format with the time "prefixed" to allow an easier shorthand) and why this wasn't changed (passing the values in the wrong order produces a valid, though unexpected, result in most cases), but it continues to be a source of confusion for me whenever I come back to PHP from other languages or libraries.
Please note, mktime requires an integer value, if you use date("H"), date("i"), date("s") as a value, which is actually have a leading zero, you may get "A non well formed numeric value encountered" notice. so you need some tricks like this mktime( date("G"), intval(date("i")), intval(date("s"), date("n"), date("j"), date("Y") ) Since there are no minute & second without leading zero in the date function, we can use the intval() function or you can cast value type like this to force the value type. (int) date("i")
Please mind function is timezone dependent. Timezone independent funciton is gmmktime
Just a small thing to think about if you are only trying to pull the month out using mktime and date. Make sure you place a 1 into day field. Otherwise you will get incorrect dates when a month is followed by a month with less days when the day of the current month is higher then the max day of the month you are trying to find.. (Such as today being Jan 30th and trying to find the month Feb.)
Be careful passing zeros into mktime, in most cases a zero will count as the previous unit of time. The documentation explains this yet most of the comments here still use zeroes. For example, if you pass the year 2013 into mktime, with zeroes for everything else, the outcome is probably not what you are looking for. Instead of using 0's, try 1's. This makes more sense (except for minutes/seconds). Maybe not as obvious of a purpose as zeroes to other programmers, though.
Pay attention that not all days have the same number of seconds (86400s) if you are using date_default_timezone_set(..) and the used timezone has Daylight Saving Time (DST) e.g. "Europe/Berlin". Under PHP 5.5.16 I get the following results: $shortday = mktime(23,59,59, '3','29','2015') - mktime(0,0,0, '3','29','2015) + 1; // result: 82800s (86400s - 3600s) $normalDay = mktime(23,59,59, '1', '2','2015') - mktime(0,0,0, '1', '1','2015) + 1; // result: 86400s $longDay = mktime(23,59,59,'10','25','2015') - mktime(0,0,0,'10','25','2015) + 1; // result: 90000s (86400s + 3600s) Pitfall is noticeable if you are running an iterative loop with a code like: echo date( 'd.m.Y', $day ); $day = $day + 86400; // 86400 = 24*3600 - frequently used in PHP code which results in wrong date if $day reaches 2015-10-25 (end of summer time in Germany): 24.10.2015 25.10.2015 25.10.2015 // Ups! Same date twice in calendar 27.10.2015 You may workaround this by using date_default_timezone_set('UTC') where all days have the same number of seconds.
It seems mktime() doesn't return negative timestamps on Linux systems with a version of glibc
Function to generate array of dates between two dates (date range array) [EDIT BY danbrown AT php DOT net: Contains a bugfix submitted by (carlosbuz2 AT gmail DOT com) on 04-MAR-2011, with the following note: The first date in array is incorrect.]
The maximum possible date accepted by mktime() and gmmktime() is dependent on the current location time zone. For example, the 32-bit timestamp overflow occurs at 2038-01-19T03:14:08+0000Z. But if you're in a UTC -0500 time zone (such as EST in North America), the maximum accepted time before overflow (for older PHP versions on Windows) is 2038-01-18T22:14:07-0500Z, regardless of whether you're passing it to mktime() or gmmktime().
You cannot simply subtract or add month VARs using mktime to obtain previous or next months as suggested in previous user comments (at least not with a DD > 28 anyway). If the date is 03-31-2007, the following yeilds March as a previous month. Not what you wanted. mktime correctly gives you back the 3rd of March if you subtract 1 month from March 31 (there are only 28 days in Feb 07). If you are just looking to do month and year arithmetic using mktime, you can use general days like 1 or 28 to do stuff like this:
raw date to clean timestamp private function dateToTimestamp($date){ $datefrom = explode(" ", $date); $value = array(); if(strpos($datefrom[0], '-')){ //print "issplit -"; $value = explode("-", $datefrom[0]); } if(strpos($datefrom[0], '/')){ //print "issplit /"; $value = explode("/", $datefrom[0]); } /*if(){ }*/ if(strlen($value[2])==4){//13/12/2012 //int mktime([hour[minute[second[month[day[year return mktime(0, 0, 0,$value[1],$value[0],$value[2]); }else{ //2012/12/13 //int mktime([hour[minute[second[month[day[year return mktime(0, 0, 0,$value[1],$value[2],$value[0]); } }
The following function moves all the parameters in order of most significant (biggest) to least significant (smallest) order. Year is bigger than month. Month is bigger than day. Day bigger than hours... Much less confusing than mktime order.
// here is the function which returns the Unix timestamp of last date of quarter, by quarter number: function last_day_of_quarter($q) { return mktime(0, 0, 0, floor($q*3), $q == 1 || $q == 4 ? 31 : 30); }
One practical and useful example of using negative values in mktime is the following:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!