1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-26 16:12:19 +01:00
Files
2025-11-05 15:02:17 +08:00

224 lines
7.3 KiB
XML
Executable File
Raw Permalink 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.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.date" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>date</refname>
<refpurpose>格式化 Unix 时间戳</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>date</methodname>
<methodparam><type>string</type><parameter>format</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>timestamp</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
使用指定整数 <parameter>timestamp</parameter>Unix 时间戳),或者使用当前时间(如果没有指定时间戳),返回相应的指定格式的格式化字符串。换句话说,<parameter>timestamp</parameter>
是可选的,默认值是 <function>time</function>
</para>
<warning>
<para>
Unix 时间戳不处理时区。使用 <classname>DateTimeImmutable</classname> 及其
<methodname>DateTimeInterface::format</methodname> 格式化方法来格式化携带时区的日期/时间信息。
</para>
</warning>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>format</parameter></term>
<listitem>
<para>
<methodname>DateTimeInterface::format</methodname> 接受的格式。
</para>
<note>
<simpara>
<function>date</function> 将始终生成 <literal>000000</literal> 作为微秒,因为它采用 <type>int</type>
参数,而如果 <classname>DateTimeInterface</classname> 类型的对象是用微秒创建的,
<methodname>DateTimeInterface::format</methodname> 支持微秒。
</simpara>
</note>
</listitem>
</varlistentry>
&date.timestamp.description;
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回格式化后的日期字符串。
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
&date.timezone.errors.description;
</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>timestamp</parameter> 允许为 null。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>date</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
// 设置要使用的默认时区。
date_default_timezone_set('UTC');
// 打印类似Monday
echo date("l") . "\n";
// 打印类似Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A') . "\n";
// 打印July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000)) . "\n";
/* 使用格式化参数中的常量 */
// 打印类似Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822) . "\n";
// 打印类似2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
]]>
</programlisting>
</example>
</para>
<para>
可以对格式化字符串中的解析字符,在其前面添加反斜线来防止解析。如果带有反斜线的字符已经是特殊字符,需要对反斜线进行再次转义。
<example>
<title><function>date</function> 中的转义字符</title>
<programlisting role="php">
<![CDATA[
<?php
// 打印类似: Wednesday the 15th
echo date('l \t\h\e jS');
]]>
</programlisting>
</example>
</para>
<para>
<function>date</function>
格式化的一些示例。请注意,即使是对于当前来说并不具有特殊含义的字符,也要像对待具有特殊含义的字符那样进行转义,以避免产生非预期的值,因为可能在将来的
PHP 版本中,这些字符会被赋予特殊的含义。进行转义的时候,请确保使用单引号,以避免 \n 这样的字符被解释为换行符号。
<example>
<title><function>date</function> 格式化</title>
<programlisting role="php">
<![CDATA[
<?php
// 假设今天是 2001 年 3 月 10 日下午 5 点 16 分 18 秒,
// 并且位于山区标准时间MST时区
date_default_timezone_set("America/Phoenix");
echo date("F j, Y, g:i a") . "\n"; // March 10, 2001, 5:16 pm
echo date("m.d.y") . "\n"; // 03.10.01
echo date("j, n, Y") . "\n"; // 10, 3, 2001
echo date("Ymd") . "\n"; // 20010310
echo date('h-i-s, j-m-y, it is w Day') . "\n"; // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
echo date('\i\t \i\s \t\h\e jS \d\a\y.') . "\n"; // it is the 10th day.
echo date("D M j G:i:s T Y") . "\n"; // Sat Mar 10 17:16:18 MST 2001
echo date('H:m:s \m \i\s\ \m\o\n\t\h') . "\n"; // 17:03:18 m is month
echo date("H:i:s") . "\n"; // 17:16:18
echo date("Y-m-d H:i:s") . "\n"; // 2001-03-10 17:16:18MySQL DATETIME 格式)
]]>
</programlisting>
</example>
</para>
<para>
如果需要将日期时间格式化为其他语言,使用
<methodname>IntlDateFormatter::format</methodname>
而不是 <function>date</function>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
使用 <function>strtotime</function>
将字符串表示的日期转换为时间戳。另外,一些数据库产品也提供了将日期格式转换为时间戳的函数(例如
MySQL 中的 <link xlink:href="&url.mysql.docs.date;">UNIX_TIMESTAMP</link> 函数)。
</para>
</note>
<tip>
<para>
请求的开始时间可以从变量 <varname>$_SERVER['REQUEST_TIME']</varname> 中获取。
</para>
</tip>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><methodname>DateTimeImmutable::__construct</methodname></member>
<member><methodname>DateTimeInterface::format</methodname></member>
<member><function>gmdate</function></member>
<member><function>idate</function></member>
<member><function>getdate</function></member>
<member><function>getlastmod</function></member>
<member><function>mktime</function></member>
<member><methodname>IntlDateFormatter::format</methodname></member>
<member><function>time</function></member>
<member><link linkend="datetimeinterface.constants.types">预定义 DateTime 常量</link></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
-->