uTools-Manuals/docs/php/mktime.html
2019-04-28 19:00:34 +08:00

339 lines
20 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>取得一个日期的 Unix 时间戳</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.mktime" class="refentry">
<div class="refnamediv">
<h1 class="refname">mktime</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">mktime</span> &mdash; <span class="dc-title">取得一个日期的 Unix 时间戳</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.mktime-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>mktime</strong></span>
([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$hour</code><span class="initializer"> = date(&quot;H&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$minute</code><span class="initializer"> = date(&quot;i&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$second</code><span class="initializer"> = date(&quot;s&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$month</code><span class="initializer"> = date(&quot;n&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$day</code><span class="initializer"> = date(&quot;j&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$year</code><span class="initializer"> = date(&quot;Y&quot;)</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$is_dst</code><span class="initializer"> = -1</span></span>
]]]]]]] ) : <span class="type">int</span></div>
<p class="para rdfs-comment">
根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从
Unix 纪元January 1 1970 00:00:00 GMT到给定时间的秒数。
</p>
<p class="para">
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
</p>
</div>
<div class="refsect1 notes" id="refsect1-function.mktime-notes">
<h3 class="title">注释</h3>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
As of PHP 5.1, when called with no arguments, <span class="function"><strong>mktime()</strong></span>
throws an <strong><code>E_STRICT</code></strong> notice: use the
<span class="function"><a href="time.html" class="function">time()</a></span> function instead.
</p>
</p></blockquote>
</div>
<div class="refsect1 parameters" id="refsect1-function.mktime-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">hour</code></dt>
<dd>
<p class="para">
小时数。
The number of the hour relative to the start of the day determined by
<code class="parameter">month</code>, <code class="parameter">day</code> and <code class="parameter">year</code>.
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).
</p>
</dd>
<dt>
<code class="parameter">minute</code></dt>
<dd>
<p class="para">
分钟数。
The number of the minute relative to the start of the <code class="parameter">hour</code>.
Negative values reference the minute in the previous hour.
Values greater than 59 reference the appropriate minute in the following hour(s).
</p>
</dd>
<dt>
<code class="parameter">second</code></dt>
<dd>
<p class="para">
秒数(一分钟之内)。
The number of seconds relative to the start of the <code class="parameter">minute</code>.
Negative values reference the second in the previous minute.
Values greater than 59 reference the appropriate second in the following minute(s).
</p>
</dd>
<dt>
<code class="parameter">month</code></dt>
<dd>
<p class="para">
月份数。
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).
</p>
</dd>
<dt>
<code class="parameter">day</code></dt>
<dd>
<p class="para">
天数。
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).
</p>
</dd>
<dt>
<code class="parameter">year</code></dt>
<dd>
<p class="para">
年份数可以是两位或四位数字0-69 对应于 2000-206970-100
对应于 1970-2000。在如今系统中普遍把 time_t 作为一个 32
位有符号整数的情况下,<code class="parameter">year</code>
的合法范围是 1901 到 2038 之间,不过此限制自
PHP 5.1.0 起已被克服了。
</p>
</dd>
<dt>
<code class="parameter">is_dst</code></dt>
<dd>
<p class="para">
本参数可以设为 1表示正处于夏时制时间DST0
表示不是夏时制,或者 -1默认值表示不知道是否是夏时制。如果未知PHP
会尝试自己搞明白。这可能产生不可预知(但并非不正确)的结果。如果
PHP 运行的系统中启用了 DST 或者 <code class="parameter">is_dst</code>
设为 1某些时间是无效的。例如 DST 自 2:00 生效,则所有处于
2:00 到 3:00 之间的时间都无效,<span class="function"><strong>mktime()</strong></span>
会返回一个未定义(通常为负)的值。某些系统(例如
Solaris 8的 DST 在午夜生效,则 DST 生效当天的 0:30
会被计算为前一天的 23:30。
</p>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
自 PHP 5.1.0 起,本参数已被废弃。应该使用新的时区处理特性来替代。
</p>
</p></blockquote>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
PHP 7.0.0 起,此参数已经被移除。
</p>
</p></blockquote>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.mktime-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
<span class="function"><strong>mktime()</strong></span> 根据给出的参数返回 Unix
时间戳。如果参数非法,本函数返回
<strong><code>FALSE</code></strong>(在 PHP 5.1 之前返回 <em>-1</em>)。
</p>
</div>
<div class="refsect1 errors" id="refsect1-function.mktime-errors">
<h3 class="title">错误/异常</h3>
<p class="para">
在每 次调用日期/时间函数时,如果时区无效则会引发 <strong><code>E_NOTICE</code></strong> 错误,如果使用系统设定值或 <var class="varname"><var class="varname">TZ</var></var>
环境变量,则会引发 <strong><code>E_STRICT</code></strong><strong><code>E_WARNING</code></strong> 消息。参见
<span class="function"><a href="date_default_timezone_set.html" class="function">date_default_timezone_set()</a></span></p>
</div>
<div class="refsect1 changelog" id="refsect1-function.mktime-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>7.0.0</td>
<td>
<code class="parameter">is_dst</code>参数已经被移除。
</td>
</tr>
<tr>
<td>5.3.0</td>
<td>
<span class="function"><strong>mktime()</strong></span> now throws <strong><code>E_DEPRECATED</code></strong> notice
if the <code class="parameter">is_dst</code> parameter is used.
</td>
</tr>
<tr>
<td>5.1.0</td>
<td>
<code class="parameter">is_dst</code> 参数被废弃。出错时函数返回
<strong><code>FALSE</code></strong> 而不再是 <em>-1</em>。修正了本函数可以接受年月日参数全为零。
</td>
</tr>
<tr>
<td>5.1.0</td>
<td>
When called with no arguments, <span class="function"><strong>mktime()</strong></span> throws
<strong><code>E_STRICT</code></strong> notice. Use the
<span class="function"><a href="time.html" class="function">time()</a></span> function instead.
</td>
</tr>
<tr><td>5.1.0</td><td><p class="para">
现在发布 <strong><code>E_STRICT</code></strong><strong><code>E_NOTICE</code></strong>
时区错误。</p></td></tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.mktime-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-2727">
<p><strong>Example #1 基本例子 </strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;the&nbsp;default&nbsp;timezone&nbsp;to&nbsp;use.&nbsp;Available&nbsp;as&nbsp;of&nbsp;PHP&nbsp;5.1<br /></span><span style="color: #0000BB">date_default_timezone_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'UTC'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Prints:&nbsp;July&nbsp;1,&nbsp;2000&nbsp;is&nbsp;on&nbsp;a&nbsp;Saturday<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"July&nbsp;1,&nbsp;2000&nbsp;is&nbsp;on&nbsp;a&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"l"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2000</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Prints&nbsp;something&nbsp;like:&nbsp;2006-04-05T01:02:03+00:00<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2006</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-2728">
<p><strong>Example #2 <span class="function"><strong>mktime()</strong></span> 例子</strong></p>
<div class="example-contents"><p>
<span class="function"><strong>mktime()</strong></span>
在做日期计算和验证方面很有用,它会自动计算超出范围的输入的正确值。例如下面例子中每一行都会产生字符串
&quot;Jan-01-1998&quot;
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">32</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1997</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1997</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1998</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M-d-Y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">98</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-2729">
<p><strong>Example #3 下个月的最后一天</strong></p>
<div class="example-contents"><p>
任何给定月份的最后一天都可以被表示为下个月的第 &quot;0&quot;
天,而不是 -1 天。下面两个例子都会产生字符串
&quot;The last day in Feb 2000 is: 29&quot;
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$lastday&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"Last&nbsp;day&nbsp;in&nbsp;Feb&nbsp;2000&nbsp;is:&nbsp;%d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$lastday</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lastday&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">31</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2000</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"Last&nbsp;day&nbsp;in&nbsp;Feb&nbsp;2000&nbsp;is:&nbsp;%d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$lastday</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 notes" id="refsect1-function.mktime-notes">
<h3 class="title">注释</h3>
<div class="caution"><strong class="caution">Caution</strong>
<p class="para">
在 PHP 5.1.0 之前,在任何已知 Windows
版本以及一些其它系统下不支持负的时间戳。因此年份的有效范围限制为
1970 到 2038。
</p>
</div>
</div>
<div class="refsect1 seealso" id="refsect1-function.mktime-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="checkdate.html" class="function" rel="rdfs-seeAlso">checkdate()</a> - 验证一个格里高里日期</span></li>
<li class="member"><span class="function"><a href="gmmktime.html" class="function" rel="rdfs-seeAlso">gmmktime()</a> - 取得 GMT 日期的 UNIX 时间戳</span></li>
<li class="member"><span class="function"><a href="date.html" class="function" rel="rdfs-seeAlso">date()</a> - 格式化一个本地时间/日期</span></li>
<li class="member"><span class="function"><a href="time.html" class="function" rel="rdfs-seeAlso">time()</a> - 返回当前的 Unix 时间戳</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>