reference/datetime/examples.xml
8cdc6621f9826d04abc3e50438c010804d7e8683
...
...
@@ -5,14 +5,14 @@
5
5
&reftitle.examples;
6
6

7
7
<section xml:id="datetime.examples-arithmetic">
8
-
<title>DateTime Arithmetic</title>
8
+
<title>Date/Time Arithmetic</title>
9
9
<para>
10
-
The following examples show some pitfalls of DateTime arithmetic with regard
10
+
The following examples show some pitfalls of Date/Time arithmetic with regard
11
11
to DST transitions and months having different numbers of days.
12
12
</para>
13
13
<para>
14
14
<example>
15
-
<title>DateTime::add/sub add intervals which cover elapsed time</title>
15
+
<title>DateTimeImmutable::add/sub add intervals which cover elapsed time</title>
16
16
<simpara>
17
17
Adding PT24H over a DST transition will appear to add 23/25 hours (for
18
18
most timezones).
...
...
@@ -20,9 +20,9 @@
20
20
<programlisting role="php">
21
21
<![CDATA[
22
22
<?php
23
-
$dt = new DateTime("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
23
+
$dt = new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
24
24
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
25
-
$dt->add(new DateInterval("PT3H"));
25
+
$dt = $dt->add(new DateInterval("PT3H"));
26
26
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
27
27
?>
28
28
]]>
...
...
@@ -38,7 +38,7 @@ End: 2015-11-01 02:00:00 -05:00
38
38
</para>
39
39
<para>
40
40
<example>
41
-
<title>DateTime::modify and strtotime increment or decrement individual component values</title>
41
+
<title>DateTimeImmutable::modify and strtotime increment or decrement individual component values</title>
42
42
<simpara>
43
43
Adding +24 hours over a DST transition will add exactly 24 hours as seen in
44
44
the date/time string (unless the start or end time is on a transition
...
...
@@ -47,9 +47,9 @@ End: 2015-11-01 02:00:00 -05:00
47
47
<programlisting role="php">
48
48
<![CDATA[
49
49
<?php
50
-
$dt = new DateTime("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
50
+
$dt = new DateTimeImmutable("2015-11-01 00:00:00", new DateTimeZone("America/New_York"));
51
51
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
52
-
$dt->modify("+24 hours");
52
+
$dt = $dt->modify("+24 hours");
53
53
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
54
54
?>
55
55
]]>
...
...
@@ -74,15 +74,15 @@ End: 2015-11-02 00:00:00 -05:00
74
74
<![CDATA[
75
75
<?php
76
76
echo "Normal year:\n"; // February has 28 days
77
-
$dt = new DateTime("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
77
+
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
78
78
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
79
-
$dt->modify("+1 month");
79
+
$dt = $dt->modify("+1 month");
80
80
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
81
81

82
82
echo "Leap year:\n"; // February has 29 days
83
-
$dt = new DateTime("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
83
+
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
84
84
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
85
-
$dt->modify("+1 month");
85
+
$dt = $dt->modify("+1 month");
86
86
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
87
87
?>
88
88
]]>
...
...
@@ -100,21 +100,21 @@ End: 2016-03-02 00:00:00 -05:00
100
100
</screen>
101
101
<simpara>
102
102
To get the last day of the next month (i.e. to prevent the overflow),
103
-
the <literal>last day of</literal> format is available as of PHP 5.3.0.
103
+
the <literal>last day of</literal> format is available.
104
104
</simpara>
105
105
<programlisting role="php">
106
106
<![CDATA[
107
107
<?php
108
108
echo "Normal year:\n"; // February has 28 days
109
-
$dt = new DateTime("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
109
+
$dt = new DateTimeImmutable("2015-01-31 00:00:00", new DateTimeZone("America/New_York"));
110
110
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
111
-
$dt->modify("last day of next month");
111
+
$dt = $dt->modify("last day of next month");
112
112
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
113
113

114
114
echo "Leap year:\n"; // February has 29 days
115
-
$dt = new DateTime("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
115
+
$dt = new DateTimeImmutable("2016-01-31 00:00:00", new DateTimeZone("America/New_York"));
116
116
echo "Start: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
117
-
$dt->modify("last day of next month");
117
+
$dt = $dt->modify("last day of next month");
118
118
echo "End: ", $dt->format("Y-m-d H:i:s P"), PHP_EOL;
119
119
?>
120
120
]]>
121
121