1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-24 15:52:15 +01:00
Files
archived-doc-en/reference/datetime/datetimeimmutable/sub.xml
AllenJB 3a8c3e77df Enable runnable (WASM) examples for datetime (#4887)
* Enable runnable (WASM) examples for datetime
2025-10-31 10:32:20 +00:00

182 lines
4.4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="datetimeimmutable.sub" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>DateTimeImmutable::sub</refname>
<refpurpose>
Subtracts an amount of days, months, years, hours, minutes and seconds
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="DateTimeImmutable">
<modifier role="attribute">#[\NoDiscard]</modifier>
<modifier>public</modifier> <type>DateTimeImmutable</type><methodname>DateTimeImmutable::sub</methodname>
<methodparam><type>DateInterval</type><parameter>interval</parameter></methodparam>
</methodsynopsis>
<para>
Returns a new <classname>DateTimeImmutable</classname> object, with the specified
<classname>DateInterval</classname> object subtracted from the specified
DateTimeImmutable object.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term>
<parameter>interval</parameter>
</term>
<listitem>
<para>
A <classname>DateInterval</classname> object
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&date.datetimeimmutable.return.modifiedobject;
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
If an unsupported operation is attempted, such as using a
<classname>DateInterval</classname> object representing relative time
specifications such as <literal>next weekday</literal>, a
<exceptionname>DateInvalidOperationException</exceptionname> is thrown.
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.3.0</entry>
<entry>
Now throws a
<exceptionname>DateInvalidOperationException</exceptionname> instead of
a warning when an unsupported operation is attempted.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>DateTimeImmutable::sub</function> example</title>
<para>&style.oop;</para>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTimeImmutable('2000-01-20');
$newDate = $date->sub(new DateInterval('P10D'));
echo $newDate->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2000-01-10
]]>
</screen>
</example>
<example>
<title>Further <function>DateTimeImmutable::sub</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTimeImmutable('2000-01-20');
$newDate = $date->sub(new DateInterval('PT10H30S'));
echo $newDate->format('Y-m-d H:i:s') . "\n";
$date = new DateTimeImmutable('2000-01-20');
$newDate = $date->sub(new DateInterval('P7Y5M4DT4H3M2S'));
echo $newDate->format('Y-m-d H:i:s') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2000-01-19 13:59:30
1992-08-15 19:56:58
]]>
</screen>
</example>
<example>
<title>Beware when subtracting months</title>
<programlisting role="php">
<![CDATA[
<?php
$date = new DateTimeImmutable('2001-04-30');
$interval = new DateInterval('P1M');
$newDate1 = $date->sub($interval);
echo $newDate1->format('Y-m-d') . "\n";
$newDate2 = $newDate1->sub($interval);
echo $newDate2->format('Y-m-d') . "\n";
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
2001-03-30
2001-03-02
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>DateTimeImmutable::add</function></member>
<member><function>DateTimeImmutable::diff</function></member>
<member><function>DateTimeImmutable::modify</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
-->