📄 function.mktime.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Get Unix timestamp for a date</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.microtime.html">microtime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strftime.html">strftime</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.mktime" class="refentry"> <div class="refnamediv"> <h1 class="refname">mktime</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mktime</span> — <span class="dc-title">Get Unix timestamp for a date</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>mktime</b></b></span> ([ <span class="methodparam"><span class="type">int</span> <tt class="parameter">$hour</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$minute</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$second</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$month</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$day</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$year</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$is_dst</tt></span> ]]]]]]] )</div> <p class="para rdfs-comment"> Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. </p> <p class="para"> Arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">hour</tt></i></span> <dd> <p class="para"> The number of the hour. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">minute</tt></i></span> <dd> <p class="para"> The number of the minute. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">second</tt></i></span> <dd> <p class="para"> The number of seconds past the minute. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">month</tt></i></span> <dd> <p class="para"> The number of the month. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">day</tt></i></span> <dd> <p class="para"> The number of the day. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">year</tt></i></span> <dd> <p class="para"> The number of the year, may be a two or four digit value, with values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On systems where time_t is a 32bit signed integer, as most common today, the valid range for <i><tt class="parameter">year</tt></i> is somewhere between 1901 and 2038. However, before PHP 5.1.0 this range was limited from 1970 to 2038 on some systems (e.g. Windows). </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">is_dst</tt></i></span> <dd> <p class="para"> This parameter can be set to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown whether the time is within daylight savings time or not. If it's unknown, PHP tries to figure it out itself. This can cause unexpected (but not incorrect) results. Some times are invalid if DST is enabled on the system PHP is running on or <i><tt class="parameter">is_dst</tt></i> is set to 1. If DST is enabled in e.g. 2:00, all times between 2:00 and 3:00 are invalid and <b>mktime()</b> returns an undefined (usually negative) value. Some systems (e.g. Solaris 8) enable DST at midnight so time 0:30 of the day when DST is enabled is evaluated as 23:30 of the previous day. </p> <blockquote><p><b class="note">Note</b>: As of PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead. <br /> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <b>mktime()</b> returns the Unix timestamp of the arguments given. If the arguments are invalid, the function returns <b><tt>FALSE</tt></b> (before PHP 5.1 it returned <i>-1</i>). </p> </div> <div class="refsect1 errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para">Every call to a date/time function will generate a <b><tt>E_NOTICE</tt></b> if the time zone is not valid, and/or a <b><tt>E_STRICT</tt></b> messageif using the system settings or the <var class="varname">TZ</var> environmentvariable. See also <a href="function.date-default-timezone-set.html" class="function">date_default_timezone_set()</a></p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.3.0</td> <td colspan="1" rowspan="1" align="left"> <b>mktime()</b> now throws <i>E_DEPRECATED</i> notice if the <i><tt class="parameter">is_dst</tt></i> parameter is used. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">3.0.10</td> <td colspan="1" rowspan="1" align="left">Added <i><tt class="parameter">is_dst</tt></i> parameter</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.1.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">is_dst</tt></i> parameter became deprecated. Made the function return <b><tt>FALSE</tt></b> on error, instead of <i>-1</i>. Fixed the function to accept the year, month and day to be all passed as zero. </td> </tr> <tr valign="middle"><td colspan="1" rowspan="1" align="left">5.1.0</td><td colspan="1" rowspan="1" align="left"><p class="para">Now issues the <b><tt>E_STRICT</tt></b> and <b><tt>E_NOTICE</tt></b> time zone errors.</p></td></tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>mktime()</b> example</b></p> <div class="example-contents"><p> <b>mktime()</b> is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. For example, each of the following lines produces the string "Jan-01-1998". </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #0000BB">32</span><span style="color: #007700">, </span><span style="color: #0000BB">1997</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">13</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1997</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1998</span><span style="color: #007700">));<br />echo </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">98</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Last day of next month</b></p> <div class="example-contents"><p> The last day of any given month can be expressed as the "0" day of the next month, not the -1 day. Both of the following examples will produce the string "The last day in Feb 2000 is: 29". </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$lastday </span><span style="color: #007700">= </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"Last day in Feb 2000 is: %d"</span><span style="color: #007700">, </span><span style="color: #0000BB">$lastday</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lastday </span><span style="color: #007700">= </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, -</span><span style="color: #0000BB">31</span><span style="color: #007700">, </span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"Last day in Feb 2000 is: %d"</span><span style="color: #007700">, </span><span style="color: #0000BB">$lastday</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <div class="caution"><b class="caution">Caution</b> <p class="para"> Before PHP 5.1.0, negative timestamps were not supported under any known version of Windows and some other systems as well. Therefore the range of valid years was limited to 1970 through 2038. </p> </div> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.gmmktime.html" class="function" rel="rdfs-seeAlso">gmmktime()</a></li> <li class="member"><a href="function.date.html" class="function" rel="rdfs-seeAlso">date()</a></li> <li class="member"><a href="function.time.html" class="function" rel="rdfs-seeAlso">time()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.microtime.html">microtime</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.strftime.html">strftime</a></div> <div class="up"><a href="ref.datetime.html">Date/Time Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -