mirror of
https://github.com/php/doc-zh.git
synced 2026-04-27 10:16:00 +02:00
300 lines
9.5 KiB
XML
Executable File
300 lines
9.5 KiB
XML
Executable File
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: HonestQiao Status: ready -->
|
||
<!-- CREDITS: mowangjuanzi, Luffy -->
|
||
<refentry xml:id="function.mktime" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>mktime</refname>
|
||
<refpurpose>取得一个日期的 Unix 时间戳</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type class="union"><type>int</type><type>false</type></type><methodname>mktime</methodname>
|
||
<methodparam><type>int</type><parameter>hour</parameter></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>minute</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>second</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>month</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>day</parameter><initializer>&null;</initializer></methodparam>
|
||
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>year</parameter><initializer>&null;</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
根据给出的参数返回 Unix 时间戳。时间戳是一个长整数,包含了从
|
||
Unix 纪元(January 1 1970 00:00:00 GMT)到给定时间的秒数。
|
||
</para>
|
||
<para>
|
||
任何可选或为 &null; 的参数都将根据本地日期和时间设置为当前值。
|
||
</para>
|
||
<warning>
|
||
<para>
|
||
请注意,参数的顺序是很奇怪:<parameter>month</parameter>、<parameter>day</parameter>、<parameter>year</parameter>,而不是更合理的
|
||
<parameter>year</parameter>、<parameter>month</parameter>、<parameter>day</parameter> 顺序。
|
||
</para>
|
||
</warning>
|
||
<simpara>
|
||
不带参数调用 <function>mktime</function> 已经不受支持,会导致 <classname>ArgumentCountError</classname>。
|
||
<function>time</function> 可以用来获取当前时间戳。
|
||
</simpara>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>hour</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
相对于一天开始的小时数,由 <parameter>month</parameter>、<parameter>day</parameter> 和
|
||
<parameter>year</parameter> 确定。负值是指当天零点前的小时。值大于 23 是指接下来几天的适当小时。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>minute</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
相对于 <parameter>hour</parameter> 开始的分钟数。负值指的是上个小时的分钟。大于 59
|
||
的值将指向接下来几个小时的适当分钟。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>second</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
相对于 <parameter>minute</parameter> 开始的秒数。负值是指前一分钟的秒数。大于 59
|
||
的值是指接下来几分钟的适当秒。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>month</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
相对于去年年底的月份数。值 1 到 12 是指相关年份的正常日历月份。小于 1
|
||
的值(包括负值)按相反的顺序指向去年的月份,因此 0 是 12 月,-1 是 11
|
||
月,以此类推。值大于 12 是指接下来几年的适当月份。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>day</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
相对于上个月月底的天数。值 1 到 28、29、30 或 31(取决于月份)是指相关月份的正常天数。小于
|
||
1 的值(包括负值)是指上个月的天数,因此 0 是上个月的最后一天,-1
|
||
是倒数第二天,以此类推。大于相关月份天数的值是指接下来几个月的适当某一天。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>year</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
年份数,可以是两位或四位数字,0-69 对应于 2000-2069,70-100
|
||
对应于 1970-2000。在如今系统中普遍把 time_t 作为一个 32
|
||
位有符号整数的情况下,<parameter>year</parameter>
|
||
的合法范围是 1901 到 2038 之间。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
<function>mktime</function> 根据给出的参数返回 Unix 时间戳,如果时间戳不适合 PHP 整数则返回 &false;。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<para>
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>8.0.0</entry>
|
||
<entry>
|
||
<parameter>hour</parameter> 不再可选。如果需要 Unix 时间戳,可以使用 <function>time</function>。
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry>8.0.0</entry>
|
||
<entry>
|
||
现在 <parameter>minute</parameter>、<parameter>second</parameter>、<parameter>month</parameter>、<parameter>day</parameter>
|
||
和 <parameter>year</parameter> 允许为 null。
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title>基本示例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// 设置使用的默认时区。
|
||
date_default_timezone_set('UTC');
|
||
|
||
// 打印:July 1, 2000 is on a Saturday
|
||
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)) . "\n";
|
||
|
||
// 打印类似的东西:2006-04-05T01:02:03+00:00
|
||
echo date('c', mktime(1, 2, 3, 4, 5, 2006)) . "\n";
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
July 1, 2000 is on a Saturday
|
||
2006-04-05T01:02:03+00:00
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title><function>mktime</function> 示例</title>
|
||
<para>
|
||
<function>mktime</function>
|
||
在做日期计算和验证方面很有用,它会自动计算超出范围的输入的正确值。例如下面示例中每一行都会产生字符串
|
||
"Jan-01-1998"。
|
||
</para>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
date_default_timezone_set('America/New_York');
|
||
|
||
echo date("c", mktime(0, 0, 0, 12, 32, 1997)) . "\n";
|
||
echo date("c", mktime(0, 0, 0, 13, 1, 1997)) . "\n";
|
||
echo date("c", mktime(0, 0, 0, 1, 1, 1998)) . "\n";
|
||
echo date("c", mktime(0, 0, 0, 1, 1, 98)) . "\n";
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
1998-01-01T00:00:00-05:00
|
||
1998-01-01T00:00:00-05:00
|
||
1998-01-01T00:00:00-05:00
|
||
1998-01-01T00:00:00-05:00
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>使用 mktime 查找相对日期</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
date_default_timezone_set('Asia/Tokyo');
|
||
|
||
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
|
||
print date('c', $tomorrow) . "\n";
|
||
|
||
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
|
||
print date('c', $lastmonth) . "\n";
|
||
|
||
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1) . "\n";
|
||
print date('c', $nextyear) . "\n";
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
2025-09-30T00:00:00+09:00
|
||
2025-08-29T00:00:00+09:00
|
||
2026-09-29T00:00:00+09:00
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
<note>
|
||
<para>
|
||
由于夏令时的存在,这种方法比简单地对时间戳加减一天或一个月的秒数更加可靠。
|
||
</para>
|
||
</note>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>下个月的最后一天</title>
|
||
<para>
|
||
任何给定月份的最后一天都可以被表示为下个月的第 "0"
|
||
天,而不是 -1 天。下面两个示例都会产生字符串
|
||
"The last day in Feb 2000 is: 29"。
|
||
</para>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$lastday = mktime(0, 0, 0, 3, 0, 2000);
|
||
echo 'Last day in Feb 2000 is: ', date('d', $lastday) . "\n";
|
||
|
||
$lastday = mktime(0, 0, 0, 4, -31, 2000);
|
||
echo 'Last day in Feb 2000 is: ', date('d', $lastday) . "\n";
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
Last day in Feb 2000 is: 29
|
||
Last day in Feb 2000 is: 29
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><classname>DateTimeImmutable</classname> 类</member>
|
||
<member><function>checkdate</function></member>
|
||
<member><function>gmmktime</function></member>
|
||
<member><function>date</function></member>
|
||
<member><function>time</function></member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
</refentry>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|