1--TEST--
2Test DateTimeImmutable::__serialize and DateTimeImmutable::__unserialize
3--FILE--
4<?php
5//Set the default time zone
6date_default_timezone_set("Europe/London");
7
8$d = new DateTimeImmutable("2022-04-14 11:27:42");
9echo "Original object:\n";
10var_dump($d);
11
12echo "\n\nSerialised object:\n";
13$s = serialize($d);
14var_dump($s);
15
16echo "\n\nUnserialised object:\n";
17$e = unserialize($s);
18var_dump($e);
19
20echo "\n\nCalling __serialize manually:\n";
21var_dump($d->__serialize());
22
23echo "\n\nCalling __unserialize manually:\n";
24$d = new DateTimeImmutable;
25$d->__unserialize(
26	[
27		'date' => '2022-04-14 11:27:42.541106',
28		'timezone_type' => 3,
29		'timezone' => 'UTC',
30	]
31);
32var_dump($d);
33
34echo "\n\nCalling __unserialize a few more times, with abbreviations:\n";
35$d->__unserialize(
36	[
37		'date' => '2022-04-14 11:27:42.541106',
38		'timezone_type' => 2,
39		'timezone' => 'CEST',
40	]
41);
42var_dump($d);
43$d->__unserialize(
44	[
45		'date' => '2022-04-14 11:27:42.541106',
46		'timezone_type' => 1,
47		'timezone' => '+0130',
48	]
49);
50var_dump($d);
51
52?>
53--EXPECTF--
54Original object:
55object(DateTimeImmutable)#%d (%d) {
56  ["date"]=>
57  string(26) "2022-04-14 11:27:42.000000"
58  ["timezone_type"]=>
59  int(3)
60  ["timezone"]=>
61  string(13) "Europe/London"
62}
63
64
65Serialised object:
66string(135) "O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2022-04-14 11:27:42.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}"
67
68
69Unserialised object:
70object(DateTimeImmutable)#%d (%d) {
71  ["date"]=>
72  string(26) "2022-04-14 11:27:42.000000"
73  ["timezone_type"]=>
74  int(3)
75  ["timezone"]=>
76  string(13) "Europe/London"
77}
78
79
80Calling __serialize manually:
81array(3) {
82  ["date"]=>
83  string(26) "2022-04-14 11:27:42.000000"
84  ["timezone_type"]=>
85  int(3)
86  ["timezone"]=>
87  string(13) "Europe/London"
88}
89
90
91Calling __unserialize manually:
92object(DateTimeImmutable)#%d (%d) {
93  ["date"]=>
94  string(26) "2022-04-14 11:27:42.541106"
95  ["timezone_type"]=>
96  int(3)
97  ["timezone"]=>
98  string(3) "UTC"
99}
100
101
102Calling __unserialize a few more times, with abbreviations:
103object(DateTimeImmutable)#%d (%d) {
104  ["date"]=>
105  string(26) "2022-04-14 11:27:42.541106"
106  ["timezone_type"]=>
107  int(2)
108  ["timezone"]=>
109  string(4) "CEST"
110}
111object(DateTimeImmutable)#%d (%d) {
112  ["date"]=>
113  string(26) "2022-04-14 11:27:42.541106"
114  ["timezone_type"]=>
115  int(1)
116  ["timezone"]=>
117  string(6) "+01:30"
118}
119