1
0
mirror of https://github.com/php/doc-ru.git synced 2026-04-25 00:08:24 +02:00
Files
archived-doc-ru/reference/datetime/datetimeinterface/diff.xml
T
Mikhail Alferov 71033720d9 Update datetime* to En (#1131)
* Update book.xml to en

* Update dateperiod.xml to en

* Update formats.xml to en

* Update construct.xml to en

* Update createfromdatestring.xml to en

* Update format.xml to en

* Update createfromiso8601string.xml to en

* Update getdateinterval.xml to en

* Update getenddate.xml to en

* Update getrecurrences.xml to en

* Update getstartdate.xml to en

* Update dateperiod.xml

* Update formats.xml

* Update construct.xml

* Update createfromdatestring.xml

* Update format.xml

* Update createfromiso8601string.xml

* Update getdateinterval.xml

* Update getenddate.xml

* Update getrecurrences.xml

* Update getstartdate.xml

* Update createfromimmutable.xml to en

* Update createfromimmutable.xml

* Update createfromimmutable.xml

* Update createfrominterface.xml to en

* Update modify.xml to en

* Update settimezone.xml to en

* Update construct.xml to en

* Update createfrominterface.xml

* Update createfrominterface.xml to en

* Update createfrominterface.xml

* Update createfrommutable.xml to en

* Update getlasterrors.xml to en

* Update modify.xml to en

* Update setdate.xml to en

* Update setisodate.xml to en

* Update settime.xml to en

* Update settimestamp.xml to en

* Update settimezone.xml to en

* Update sub.xml to en

* Update diff.xml to en

* Update format.xml to En

* Update getoffset.xml to en

* Update format.xml

* Update gettimestamp.xml to en

* Update gettimezone.xml to en

* Update serialize.xml to en

* Update unserialize.xml to en

* Update construct.xml to en

* Update getlocation.xml to en

* Update getoffset.xml to en

* Update getoffset.xml Само обновление

* Update gettransitions.xml to en

* Update listabbreviations.xml to en

* Update checkdate.xml to en

* Update date-default-timezone-get.xml to en

* Update date-default-timezone-set.xml to en

* Update date-parse-from-format.xml to en

* Update date-parse.xml to en

* Update date-parse-from-format.xml

* Update date-sun-info.xml to en

* Update date-sunrise.xml to en

* Update date-sunset.xml to en

* Update date.xml to en

* Update getdate.xml to en

* Update gettimeofday.xml to en

* Update date-parse-from-format.xml

* Update getoffset.xml

* Update gettransitions.xml

* Update diff.xml

* Update getoffset.xml

* Update gmdate.xml to en

* Update gmmktime.xml to en

* Update gmstrftime.xml to en

* Update idate.xml to en

* Update localtime.xml to en

* Update microtime.xml  to en

* Update microtime.xml

* Update mktime.xml to en

* Update strftime.xml to en

* Update strptime.xml to en

* Update strtotime.xml to en

* Update time.xml to en

* Update timezone-name-from-abbr.xml to en

* Update timezone-version-get.xml to en

* Update construct.xml Sync rev ID

* Update getlasterrors.xml stync rev id

* Update gettransitions.xml fix xml styntax error

* Update idate.xml fix xml syntax error
2025-11-25 21:49:28 +03:00

241 lines
8.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: tmn Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="datetime.diff" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>DateTimeInterface::diff</refname>
<refname>DateTimeImmutable::diff</refname>
<refname>DateTime::diff</refname>
<refname>date_diff</refname>
<refpurpose>Возвращает разницу между двумя объектами даты и времени</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>&style.oop;</para>
<methodsynopsis role="DateTimeInterface">
<modifier>public</modifier> <type>DateInterval</type><methodname>DateTimeInterface::diff</methodname>
<methodparam><type>DateTimeInterface</type><parameter>targetObject</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>absolute</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<methodsynopsis role="DateTimeImmutable">
<modifier>public</modifier> <type>DateInterval</type><methodname>DateTimeImmutable::diff</methodname>
<methodparam><type>DateTimeInterface</type><parameter>targetObject</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>absolute</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<methodsynopsis role="DateTime">
<modifier>public</modifier> <type>DateInterval</type><methodname>DateTime::diff</methodname>
<methodparam><type>DateTimeInterface</type><parameter>targetObject</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>absolute</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<para>&style.procedural;</para>
<methodsynopsis>
<type>DateInterval</type><methodname>date_diff</methodname>
<methodparam><type>DateTimeInterface</type><parameter>baseObject</parameter></methodparam>
<methodparam><type>DateTimeInterface</type><parameter>targetObject</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>absolute</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<para>
Метод возвращает разницу между двумя объектами, совместимыми с интерфейсом <classname>DateTimeInterface</classname>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>datetime</parameter></term>
<listitem>
<para>
Дата, с которой требуется сравнить исходную.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>absolute</parameter></term>
<listitem>
<para>
Параметр определяет, переводить ли интервал в абсолютную величину.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Метод возвращает объект <classname>DateInterval</classname>,
который представляет разницу между двумя датами.
</para>
<para>
Параметр <parameter>absolute</parameter> влияет только
на свойство <parameter>invert</parameter>
объекта <classname>DateInterval</classname>.
</para>
<para>
Конкретнее: метод возвращает значение — представление интервала времени,
который потребуется применить к исходному объекту <parameter>$this</parameter> или <parameter>$originObject</parameter>,
чтобы получить целевой объект <parameter>$targetObject</parameter>.
Восстановление исходной даты из целевой и интервала иногда невозможно.
</para>
<para>
Метод учитывает переходы на летнее время и поэтому иногда возвращает интервал
<literal>24 hours and 30 minutes</literal>, как во втором примере.
Для расчёта абсолютного времени
объекты <parameter>$this</parameter> или <parameter>$baseObject</parameter>
и <parameter>$targetObject</parameter> сначала преобразовывают ко времени по стандарту UTC.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Пример вычисления разницы между датами методом <function>DateTimeImmutable::diff</function></title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$origin = new DateTimeImmutable('2009-10-11');
$target = new DateTimeImmutable('2009-10-13');
$interval = $origin->diff($target);
echo $interval->format('%R%a days');
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
+2 days
]]>
</screen>
<para>&style.procedural;</para>
<programlisting role="php">
<![CDATA[
<?php
$origin = date_create('2009-10-11');
$target = date_create('2009-10-13');
$interval = date_diff($origin, $target);
echo $interval->format('%R%a days');
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
+2 days
]]>
</screen>
</example>
<example>
<title>
Пример вычисления разницы между датами методом <methodname>DateTimeInterface::diff</methodname>
при переходе на летнее время
</title>
<programlisting role="php">
<![CDATA[
<?php
$originalTime = new DateTimeImmutable("2021-10-30 09:00:00 Europe/London");
$targetTime = new DateTimeImmutable("2021-10-31 08:30:00 Europe/London");
$interval = $originalTime->diff($targetTime);
echo $interval->format("%H:%I:%S (Полных дней: %a)"), "\n";
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
24:30:00 (Полных дней: 0)
]]>
</screen>
</example>
<example>
<title>Пример вычисления разницы для диапазона дат методом <methodname>DateTimeInterface::diff</methodname></title>
<para>
Значение, которое возвращает метод, — точное количество времени, которое требуется
для перехода от времени объекта <parameter>$this</parameter> ко времени объекта <parameter>$targetObject</parameter>.
Поэтому сравнение 1 января с 31 декабря для невисокосных годов возвращает 364 дня, а не 365.
</para>
<programlisting role="php">
<![CDATA[
<?php
$originalTime = new DateTimeImmutable("2023-01-01 UTC");
$targetTime = new DateTimeImmutable("2023-12-31 UTC");
$interval = $originalTime->diff($targetTime);
echo "Полных дней: ", $interval->format("%a"), "\n";
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Полных дней: 364
]]>
</screen>
</example>
<example>
<title>Сравнение объектов <classname>DateTime</classname></title>
<note>
<para>
Объекты <classname>DateTimeImmutable</classname>
и <classname>DateTime</classname> возможно сравнивать
<link linkend="language.operators.comparison">операторами сравнения</link>.
</para>
</note>
<programlisting role="php">
<![CDATA[
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(false)
bool(true)
bool(false)
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>DateInterval::format</function></member>
<member><function>DateTime::add</function></member>
<member><function>DateTime::sub</function></member>
</simplelist>
</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
-->