mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 15:12:20 +01:00
149 lines
4.4 KiB
XML
149 lines
4.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: cd8b964b8566801265f0d287db6eb651f93be950 Maintainer: mowangjuanzi Status: ready -->
|
|
<chapter xml:id="datetime.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
&reftitle.examples;
|
|
|
|
<section xml:id="datetime.examples-arithmetic">
|
|
<title>日期/时间算法</title>
|
|
<para>
|
|
以下示例展示了有关 DST 转换和具有不同天数的月份的日期/时间算法的一些缺陷。
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>DateTimeImmutable::add/sub 添加包含经过时间的间隔</title>
|
|
<simpara>
|
|
在 DST 转换上添加 PT24H 似乎会增加 23/25 小时(大多数时区)。
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$dt = new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->add(new DateInterval("PT3H"));
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Start: 2015-11-01 00:00:00 -04:00
|
|
End: 2015-11-01 02:00:00 -05:00
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>DateTimeImmutable::modify 和 strtotime 递增/减单个组件值</title>
|
|
<simpara>
|
|
在 DST 转换上添加 +24 小时将准确添加 24 小时,如日期/时间字符串中所示(除非开始/结束时间位于转换点)。
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$dt = new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->modify("+24 hours");
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Start: 2015-11-01 00:00:00 -04:00
|
|
End: 2015-11-02 00:00:00 -05:00
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>新增/减去时间可能会溢出/下溢日期</title>
|
|
<simpara>
|
|
就像 1 月 31 日 + 1 月会导致 3 月 2 日(闰年)或 3 月 3 日(平年)。
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "Normal year:\n"; // 2 月有 28 天
|
|
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->modify("+1 month");
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
|
|
echo "Leap year:\n"; // 2 月有 29 天
|
|
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->modify("+1 month");
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Normal year:
|
|
Start: 2015-01-31 00:00:00 -05:00
|
|
End: 2015-03-03 00:00:00 -05:00
|
|
Leap year:
|
|
Start: 2016-01-31 00:00:00 -05:00
|
|
End: 2016-03-02 00:00:00 -05:00
|
|
]]>
|
|
</screen>
|
|
<simpara>
|
|
要获取下个月的最后一天(例如防止溢出),可以使用 <literal>last day of</literal> 格式。
|
|
</simpara>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "Normal year:\n"; // 2 月有 28 天
|
|
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->modify("last day of next month");
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
|
|
echo "Leap year:\n"; // 2 月有 29 天
|
|
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
|
|
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
$dt = $dt->modify("last day of next month");
|
|
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Normal year:
|
|
Start: 2015-01-31 00:00:00 -05:00
|
|
End: 2015-02-28 00:00:00 -05:00
|
|
Leap year:
|
|
Start: 2016-01-31 00:00:00 -05:00
|
|
End: 2016-02-29 00:00:00 -05:00
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
<!-- 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
|
|
-->
|