File: DateTime_serialization.phpt

package info (click to toggle)
php8.4 8.4.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 211,276 kB
  • sloc: ansic: 1,176,142; php: 35,419; sh: 11,964; cpp: 7,208; pascal: 4,951; javascript: 3,091; asm: 2,817; yacc: 2,411; makefile: 696; xml: 446; python: 301; awk: 148
file content (118 lines) | stat: -rw-r--r-- 2,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--TEST--
Test DateTime::__serialize and DateTime::__unserialize
--FILE--
<?php
//Set the default time zone
date_default_timezone_set("Europe/London");

$d = new DateTime("2022-04-14 11:27:42");
echo "Original object:\n";
var_dump($d);

echo "\n\nSerialised object:\n";
$s = serialize($d);
var_dump($s);

echo "\n\nUnserialised object:\n";
$e = unserialize($s);
var_dump($e);

echo "\n\nCalling __serialize manually:\n";
var_dump($d->__serialize());

echo "\n\nCalling __unserialize manually:\n";
$d = new DateTime;
$d->__unserialize(
	[
		'date' => '2022-04-14 11:27:42.541106',
		'timezone_type' => 3,
		'timezone' => 'UTC',
	]
);
var_dump($d);

echo "\n\nCalling __unserialize a few more times, with abbreviations:\n";
$d->__unserialize(
	[
		'date' => '2022-04-14 11:27:42.541106',
		'timezone_type' => 2,
		'timezone' => 'CEST',
	]
);
var_dump($d);
$d->__unserialize(
	[
		'date' => '2022-04-14 11:27:42.541106',
		'timezone_type' => 1,
		'timezone' => '+0130',
	]
);
var_dump($d);

?>
--EXPECTF--
Original object:
object(DateTime)#%d (%d) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}


Serialised object:
string(125) "O:8:"DateTime":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";}"


Unserialised object:
object(DateTime)#%d (%d) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}


Calling __serialize manually:
array(3) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}


Calling __unserialize manually:
object(DateTime)#%d (%d) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.541106"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}


Calling __unserialize a few more times, with abbreviations:
object(DateTime)#%d (%d) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.541106"
  ["timezone_type"]=>
  int(2)
  ["timezone"]=>
  string(4) "CEST"
}
object(DateTime)#%d (%d) {
  ["date"]=>
  string(26) "2022-04-14 11:27:42.541106"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+01:30"
}