reference/datetime/dateinterval/construct.xml
71692b6f4cace8dca72a18ccd80d4cd7305e5d4e
...
...
@@ -1,6 +1,5 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-

4
3
<refentry xml:id="dateinterval.construct" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
4
<refnamediv>
6
5
<refname>DateInterval::__construct</refname>
...
...
@@ -9,9 +8,9 @@
9
8

10
9
<refsect1 role="description">
11
10
&reftitle.description;
12
-
<constructorsynopsis role="oop">
11
+
<constructorsynopsis role="DateInterval">
13
12
<modifier>public</modifier> <methodname>DateInterval::__construct</methodname>
14
-
<methodparam><type>string</type><parameter>interval_spec</parameter></methodparam>
13
+
<methodparam><type>string</type><parameter>duration</parameter></methodparam>
15
14
</constructorsynopsis>
16
15
<para>
17
16
Creates a new DateInterval object.
...
...
@@ -23,7 +22,7 @@
23
22
<para>
24
23
<variablelist>
25
24
<varlistentry>
26
-
<term><parameter>interval_spec</parameter></term>
25
+
<term><parameter>duration</parameter></term>
27
26
<listitem>
28
27
<para>
29
28
An interval specification.
...
...
@@ -40,7 +39,7 @@
40
39
<para>
41
40
<table>
42
41
<title>
43
-
<parameter>interval_spec</parameter> Period Designators
42
+
<parameter>duration</parameter> Period Designators
44
43
</title>
45
44
<tgroup cols="2">
46
45
<thead>
...
...
@@ -65,8 +64,8 @@
65
64
<row>
66
65
<entry><literal>W</literal></entry>
67
66
<entry>
68
-
weeks. These get converted into days,
69
-
so can not be combined with <literal>D</literal>.
67
+
weeks. Converted into days.
68
+
Prior to PHP 8.0.0, can not be combined with <literal>D</literal>.
70
69
</entry>
71
70
</row>
72
71
<row>
...
...
@@ -110,8 +109,7 @@
110
109
roll-over-point (e.g. <literal>25</literal> hours is invalid).
111
110
</para>
112
111
<para>
113
-
These formats are based on the <link
114
-
xlink:href="&url.iso-8601.duration;">ISO 8601 duration
112
+
These formats are based on the <link xlink:href="&url.iso-8601.duration;">ISO 8601 duration
115
113
specification</link>.
116
114
</para>
117
115
</listitem>
...
...
@@ -123,46 +121,193 @@
123
121
<refsect1 role="errors">
124
122
&reftitle.errors;
125
123
<para>
126
-
Throws an <classname>Exception</classname> when the <parameter>interval_spec</parameter>
127
-
cannot be parsed as an interval.
124
+
Throws an <classname>DateMalformedIntervalStringException</classname> when
125
+
the <parameter>duration</parameter> cannot be parsed as an interval. Prior
126
+
to PHP 8.3, this was <exceptionname>Exception</exceptionname>.
128
127
</para>
129
128
</refsect1>
130
129

130
+
<refsect1 role="changelog">
131
+
&reftitle.changelog;
132
+
<informaltable>
133
+
<tgroup cols="2">
134
+
<thead>
135
+
<row>
136
+
<entry>&Version;</entry>
137
+
<entry>&Description;</entry>
138
+
</row>
139
+
</thead>
140
+
<tbody>
141
+
<row>
142
+
<entry>8.3.0</entry>
143
+
<entry>
144
+
Now throws
145
+
<exceptionname>DateMalformedIntervalStringException</exceptionname>
146
+
instead of <exceptionname>Exception</exceptionname>.
147
+
</entry>
148
+
</row>
149
+
<row>
150
+
<entry>8.2.0</entry>
151
+
<entry>
152
+
Only the <literal>y</literal> to <literal>f</literal>,
153
+
<literal>invert</literal>, and <literal>days</literal> will be visible,
154
+
including a new <literal>from_string</literal> boolean property.
155
+
</entry>
156
+
</row>
157
+
<row>
158
+
<entry>8.0.0</entry>
159
+
<entry>
160
+
<literal>W</literal> can be combined with <literal>D</literal>.
161
+
</entry>
162
+
</row>
163
+
</tbody>
164
+
</tgroup>
165
+
</informaltable>
166
+
</refsect1>
167
+

131
168
<refsect1 role="examples">
132
169
&reftitle.examples;
133
170
<para>
134
171
<example>
172
+
<title>Constructing and using <classname>DateInterval</classname> objects</title>
173
+
<programlisting role="php">
174
+
<![CDATA[
175
+
<?php
176
+
// Create a specific date
177
+
$someDate = \DateTime::createFromFormat("Y-m-d H:i", "2022-08-25 14:18");
178
+

179
+
// Create interval
180
+
$interval = new \DateInterval("P7D");
181
+

182
+
// Add interval
183
+
$someDate->add($interval);
184
+

185
+
// Convert interval to string
186
+
echo $interval->format("%d");
187
+
]]>
188
+
</programlisting>
189
+
&example.outputs;
190
+
<screen role="php">
191
+
7
192
+
</screen>
193
+
</example>
194
+
</para>
195
+

196
+
<para>
197
+
<example>
135
198
<title><classname>DateInterval</classname> example</title>
136
199
<programlisting role="php">
137
200
<![CDATA[
138
201
<?php
139
202

140
-
$interval = new DateInterval('P2Y4DT6H8M');
203
+
$interval = new DateInterval('P1W2D');
141
204
var_dump($interval);
142
205

143
206
?>
144
207
]]>
145
208
</programlisting>
146
-
&example.outputs;
209
+
&example.outputs.82;
147
210
<screen role="php">
148
211
<![CDATA[
149
-
object(DateInterval)#1 (8) {
212
+
object(DateInterval)#1 (10) {
150
213
["y"]=>
151
-
int(2)
214
+
int(0)
215
+
["m"]=>
216
+
int(0)
217
+
["d"]=>
218
+
int(9)
219
+
["h"]=>
220
+
int(0)
221
+
["i"]=>
222
+
int(0)
223
+
["s"]=>
224
+
int(0)
225
+
["f"]=>
226
+
float(0)
227
+
["invert"]=>
228
+
int(0)
229
+
["days"]=>
230
+
bool(false)
231
+
["from_string"]=>
232
+
bool(false)
233
+
}
234
+
]]>
235
+
</screen>
236
+
&example.outputs.8;
237
+
<screen role="php">
238
+
<![CDATA[
239
+
object(DateInterval)#1 (16) {
240
+
["y"]=>
241
+
int(0)
242
+
["m"]=>
243
+
int(0)
244
+
["d"]=>
245
+
int(9)
246
+
["h"]=>
247
+
int(0)
248
+
["i"]=>
249
+
int(0)
250
+
["s"]=>
251
+
int(0)
252
+
["f"]=>
253
+
float(0)
254
+
["weekday"]=>
255
+
int(0)
256
+
["weekday_behavior"]=>
257
+
int(0)
258
+
["first_last_day_of"]=>
259
+
int(0)
260
+
["invert"]=>
261
+
int(0)
262
+
["days"]=>
263
+
bool(false)
264
+
["special_type"]=>
265
+
int(0)
266
+
["special_amount"]=>
267
+
int(0)
268
+
["have_weekday_relative"]=>
269
+
int(0)
270
+
["have_special_relative"]=>
271
+
int(0)
272
+
}
273
+
]]>
274
+
</screen>
275
+
&example.outputs.7;
276
+
<screen role="php">
277
+
<![CDATA[
278
+
object(DateInterval)#1 (16) {
279
+
["y"]=>
280
+
int(0)
152
281
["m"]=>
153
282
int(0)
154
283
["d"]=>
155
-
int(4)
284
+
int(2)
156
285
["h"]=>
157
-
int(6)
286
+
int(0)
158
287
["i"]=>
159
-
int(8)
288
+
int(0)
160
289
["s"]=>
161
290
int(0)
291
+
["f"]=>
292
+
float(0)
293
+
["weekday"]=>
294
+
int(0)
295
+
["weekday_behavior"]=>
296
+
int(0)
297
+
["first_last_day_of"]=>
298
+
int(0)
162
299
["invert"]=>
163
300
int(0)
164
301
["days"]=>
165
302
bool(false)
303
+
["special_type"]=>
304
+
int(0)
305
+
["special_amount"]=>
306
+
int(0)
307
+
["have_weekday_relative"]=>
308
+
int(0)
309
+
["have_special_relative"]=>
310
+
int(0)
166
311
}
167
312
]]>
168
313
</screen>
...
...
@@ -183,7 +328,6 @@ object(DateInterval)#1 (8) {
183
328
</refsect1>
184
329

185
330
</refentry>
186
-

187
331
<!-- Keep this comment at the end of the file
188
332
Local variables:
189
333
mode: sgml
190
334