1--TEST--
2Test DatePeriod::__serialize and DatePeriod::__unserialize (start/end)
3--FILE--
4<?php
5date_default_timezone_set("Europe/London");
6
7$s = new DateTimeImmutable("1978-12-22 09:15:00 Europe/Amsterdam");
8$e = new DateTimeImmutable("2022-04-29 15:51:56 Europe/London");
9$i = new DateInterval('P2Y6M');
10$d = new DatePeriod($s, $i, $e);
11echo "Original object:\n";
12var_dump($d);
13
14echo "\n\nSerialised object:\n";
15$s = serialize($d);
16var_dump($s);
17
18echo "\n\nUnserialised object:\n";
19$e = unserialize($s);
20var_dump($e);
21
22echo "\n\nCalling __serialize manually:\n";
23var_dump($d->__serialize());
24
25echo "\n\nIterate of unserialised object:\n";
26foreach ( $e as $d )
27{
28	echo $d->format(DateTime::ISO8601), "\n";
29}
30?>
31--EXPECTF--
32Original object:
33object(DatePeriod)#%d (%d) {
34  ["start"]=>
35  object(DateTimeImmutable)#%d (%d) {
36    ["date"]=>
37    string(26) "1978-12-22 09:15:00.000000"
38    ["timezone_type"]=>
39    int(3)
40    ["timezone"]=>
41    string(16) "Europe/Amsterdam"
42  }
43  ["current"]=>
44  NULL
45  ["end"]=>
46  object(DateTimeImmutable)#%d (%d) {
47    ["date"]=>
48    string(26) "2022-04-29 15:51:56.000000"
49    ["timezone_type"]=>
50    int(3)
51    ["timezone"]=>
52    string(13) "Europe/London"
53  }
54  ["interval"]=>
55  object(DateInterval)#%d (%d) {
56    ["y"]=>
57    int(2)
58    ["m"]=>
59    int(6)
60    ["d"]=>
61    int(0)
62    ["h"]=>
63    int(0)
64    ["i"]=>
65    int(0)
66    ["s"]=>
67    int(0)
68    ["f"]=>
69    float(0)
70    ["invert"]=>
71    int(0)
72    ["days"]=>
73    bool(false)
74    ["from_string"]=>
75    bool(false)
76  }
77  ["recurrences"]=>
78  int(1)
79  ["include_start_date"]=>
80  bool(true)
81  ["include_end_date"]=>
82  bool(false)
83}
84
85
86Serialised object:
87string(%d) "O:10:"DatePeriod":7:{s:5:"start";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"1978-12-22 09:15:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:16:"Europe/Amsterdam";}s:7:"current";N;s:3:"end";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2022-04-29 15:51:56.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:2;s:1:"m";i:6;s:1:"d";i:0;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;s:16:"include_end_date";b:0;}"
88
89
90Unserialised object:
91object(DatePeriod)#%d (%d) {
92  ["start"]=>
93  object(DateTimeImmutable)#%d (%d) {
94    ["date"]=>
95    string(26) "1978-12-22 09:15:00.000000"
96    ["timezone_type"]=>
97    int(3)
98    ["timezone"]=>
99    string(16) "Europe/Amsterdam"
100  }
101  ["current"]=>
102  NULL
103  ["end"]=>
104  object(DateTimeImmutable)#%d (%d) {
105    ["date"]=>
106    string(26) "2022-04-29 15:51:56.000000"
107    ["timezone_type"]=>
108    int(3)
109    ["timezone"]=>
110    string(13) "Europe/London"
111  }
112  ["interval"]=>
113  object(DateInterval)#%d (%d) {
114    ["y"]=>
115    int(2)
116    ["m"]=>
117    int(6)
118    ["d"]=>
119    int(0)
120    ["h"]=>
121    int(0)
122    ["i"]=>
123    int(0)
124    ["s"]=>
125    int(0)
126    ["f"]=>
127    float(0)
128    ["invert"]=>
129    int(0)
130    ["days"]=>
131    bool(false)
132    ["from_string"]=>
133    bool(false)
134  }
135  ["recurrences"]=>
136  int(1)
137  ["include_start_date"]=>
138  bool(true)
139  ["include_end_date"]=>
140  bool(false)
141}
142
143
144Calling __serialize manually:
145array(%d) {
146  ["start"]=>
147  object(DateTimeImmutable)#%d (%d) {
148    ["date"]=>
149    string(26) "1978-12-22 09:15:00.000000"
150    ["timezone_type"]=>
151    int(3)
152    ["timezone"]=>
153    string(16) "Europe/Amsterdam"
154  }
155  ["current"]=>
156  NULL
157  ["end"]=>
158  object(DateTimeImmutable)#%d (%d) {
159    ["date"]=>
160    string(26) "2022-04-29 15:51:56.000000"
161    ["timezone_type"]=>
162    int(3)
163    ["timezone"]=>
164    string(13) "Europe/London"
165  }
166  ["interval"]=>
167  object(DateInterval)#%d (%d) {
168    ["y"]=>
169    int(2)
170    ["m"]=>
171    int(6)
172    ["d"]=>
173    int(0)
174    ["h"]=>
175    int(0)
176    ["i"]=>
177    int(0)
178    ["s"]=>
179    int(0)
180    ["f"]=>
181    float(0)
182    ["invert"]=>
183    int(0)
184    ["days"]=>
185    bool(false)
186    ["from_string"]=>
187    bool(false)
188  }
189  ["recurrences"]=>
190  int(1)
191  ["include_start_date"]=>
192  bool(true)
193  ["include_end_date"]=>
194  bool(false)
195}
196
197
198Iterate of unserialised object:
1991978-12-22T09:15:00+0100
2001981-06-22T09:15:00+0200
2011983-12-22T09:15:00+0100
2021986-06-22T09:15:00+0200
2031988-12-22T09:15:00+0100
2041991-06-22T09:15:00+0200
2051993-12-22T09:15:00+0100
2061996-06-22T09:15:00+0200
2071998-12-22T09:15:00+0100
2082001-06-22T09:15:00+0200
2092003-12-22T09:15:00+0100
2102006-06-22T09:15:00+0200
2112008-12-22T09:15:00+0100
2122011-06-22T09:15:00+0200
2132013-12-22T09:15:00+0100
2142016-06-22T09:15:00+0200
2152018-12-22T09:15:00+0100
2162021-06-22T09:15:00+0200
217