1--TEST--
2Tests for DateTime[Immutable]::createFromTimestamp
3--INI--
4date.timezone=Europe/London
5--FILE--
6<?php
7
8class MyDateTime extends DateTime {};
9class MyDateTimeImmutable extends DateTimeImmutable {};
10
11$timestamps = array(
12    1696883232,
13    -1696883232,
14    1696883232.013981,
15    -1696883232.013981,
16    0.123456,
17    -0.123456,
18    0,
19    0.0,
20    -0.0,
21    PHP_INT_MAX + 1024.0,
22    PHP_INT_MIN - 1025.0,
23    NAN,
24    +INF,
25    -INF
26);
27
28foreach ($timestamps as $ts) {
29    echo 'DateTime::createFromTimestamp(' . var_export($ts, true) . '): ';
30    try {
31        var_dump(DateTime::createFromTimestamp($ts));
32    } catch (Throwable $e) {
33        echo get_class($e) . ': ' . $e->getMessage() . "\n";
34    }
35
36    echo 'DateTimeImmutable::createFromTimestamp(' . var_export($ts, true) . '): ';
37    try {
38        var_dump(DateTimeImmutable::createFromTimestamp($ts));
39    } catch (Throwable $e) {
40        echo get_class($e) . ': ' . $e->getMessage() . "\n";
41    }
42}
43
44echo 'MyDateTime::createFromTimestamp(' . var_export(0, true) . '): ';
45try {
46    var_dump(MyDateTime::createFromTimestamp(0));
47} catch (Throwable $e) {
48    echo get_class($e) . ': ' . $e->getMessage() . "\n";
49}
50
51echo 'MyDateTimeImmutable::createFromTimestamp(' . var_export(0, true) . '): ';
52try {
53    var_dump(MyDateTimeImmutable::createFromTimestamp(0));
54} catch (Throwable $e) {
55    echo get_class($e) . ': ' . $e->getMessage() . "\n";
56}
57
58?>
59--EXPECTF--
60DateTime::createFromTimestamp(1696883232): object(DateTime)#%d (3) {
61  ["date"]=>
62  string(26) "2023-10-09 20:27:12.000000"
63  ["timezone_type"]=>
64  int(1)
65  ["timezone"]=>
66  string(6) "+00:00"
67}
68DateTimeImmutable::createFromTimestamp(1696883232): object(DateTimeImmutable)#%d (3) {
69  ["date"]=>
70  string(26) "2023-10-09 20:27:12.000000"
71  ["timezone_type"]=>
72  int(1)
73  ["timezone"]=>
74  string(6) "+00:00"
75}
76DateTime::createFromTimestamp(-1696883232): object(DateTime)#%d (3) {
77  ["date"]=>
78  string(26) "1916-03-25 03:32:48.000000"
79  ["timezone_type"]=>
80  int(1)
81  ["timezone"]=>
82  string(6) "+00:00"
83}
84DateTimeImmutable::createFromTimestamp(-1696883232): object(DateTimeImmutable)#%d (3) {
85  ["date"]=>
86  string(26) "1916-03-25 03:32:48.000000"
87  ["timezone_type"]=>
88  int(1)
89  ["timezone"]=>
90  string(6) "+00:00"
91}
92DateTime::createFromTimestamp(1696883232.013981): object(DateTime)#%d (3) {
93  ["date"]=>
94  string(26) "2023-10-09 20:27:12.013981"
95  ["timezone_type"]=>
96  int(1)
97  ["timezone"]=>
98  string(6) "+00:00"
99}
100DateTimeImmutable::createFromTimestamp(1696883232.013981): object(DateTimeImmutable)#%d (3) {
101  ["date"]=>
102  string(26) "2023-10-09 20:27:12.013981"
103  ["timezone_type"]=>
104  int(1)
105  ["timezone"]=>
106  string(6) "+00:00"
107}
108DateTime::createFromTimestamp(-1696883232.013981): object(DateTime)#%d (3) {
109  ["date"]=>
110  string(26) "1916-03-25 03:32:47.986019"
111  ["timezone_type"]=>
112  int(1)
113  ["timezone"]=>
114  string(6) "+00:00"
115}
116DateTimeImmutable::createFromTimestamp(-1696883232.013981): object(DateTimeImmutable)#%d (3) {
117  ["date"]=>
118  string(26) "1916-03-25 03:32:47.986019"
119  ["timezone_type"]=>
120  int(1)
121  ["timezone"]=>
122  string(6) "+00:00"
123}
124DateTime::createFromTimestamp(0.123456): object(DateTime)#%d (3) {
125  ["date"]=>
126  string(26) "1970-01-01 00:00:00.123456"
127  ["timezone_type"]=>
128  int(1)
129  ["timezone"]=>
130  string(6) "+00:00"
131}
132DateTimeImmutable::createFromTimestamp(0.123456): object(DateTimeImmutable)#%d (3) {
133  ["date"]=>
134  string(26) "1970-01-01 00:00:00.123456"
135  ["timezone_type"]=>
136  int(1)
137  ["timezone"]=>
138  string(6) "+00:00"
139}
140DateTime::createFromTimestamp(-0.123456): object(DateTime)#%d (3) {
141  ["date"]=>
142  string(26) "1969-12-31 23:59:59.876544"
143  ["timezone_type"]=>
144  int(1)
145  ["timezone"]=>
146  string(6) "+00:00"
147}
148DateTimeImmutable::createFromTimestamp(-0.123456): object(DateTimeImmutable)#%d (3) {
149  ["date"]=>
150  string(26) "1969-12-31 23:59:59.876544"
151  ["timezone_type"]=>
152  int(1)
153  ["timezone"]=>
154  string(6) "+00:00"
155}
156DateTime::createFromTimestamp(0): object(DateTime)#%d (3) {
157  ["date"]=>
158  string(26) "1970-01-01 00:00:00.000000"
159  ["timezone_type"]=>
160  int(1)
161  ["timezone"]=>
162  string(6) "+00:00"
163}
164DateTimeImmutable::createFromTimestamp(0): object(DateTimeImmutable)#%d (3) {
165  ["date"]=>
166  string(26) "1970-01-01 00:00:00.000000"
167  ["timezone_type"]=>
168  int(1)
169  ["timezone"]=>
170  string(6) "+00:00"
171}
172DateTime::createFromTimestamp(0.0): object(DateTime)#%d (3) {
173  ["date"]=>
174  string(26) "1970-01-01 00:00:00.000000"
175  ["timezone_type"]=>
176  int(1)
177  ["timezone"]=>
178  string(6) "+00:00"
179}
180DateTimeImmutable::createFromTimestamp(0.0): object(DateTimeImmutable)#%d (3) {
181  ["date"]=>
182  string(26) "1970-01-01 00:00:00.000000"
183  ["timezone_type"]=>
184  int(1)
185  ["timezone"]=>
186  string(6) "+00:00"
187}
188DateTime::createFromTimestamp(-0.0): object(DateTime)#%d (3) {
189  ["date"]=>
190  string(26) "1970-01-01 00:00:00.000000"
191  ["timezone_type"]=>
192  int(1)
193  ["timezone"]=>
194  string(6) "+00:00"
195}
196DateTimeImmutable::createFromTimestamp(-0.0): object(DateTimeImmutable)#%d (3) {
197  ["date"]=>
198  string(26) "1970-01-01 00:00:00.000000"
199  ["timezone_type"]=>
200  int(1)
201  ["timezone"]=>
202  string(6) "+00:00"
203}
204DateTime::createFromTimestamp(%f): DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, %f given
205DateTimeImmutable::createFromTimestamp(%f): DateRangeError: DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, %f given
206DateTime::createFromTimestamp(%f): DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, %f given
207DateTimeImmutable::createFromTimestamp(%f): DateRangeError: DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, %f given
208DateTime::createFromTimestamp(NAN): DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, NAN given
209DateTimeImmutable::createFromTimestamp(NAN): DateRangeError: DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, NAN given
210DateTime::createFromTimestamp(INF): DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, INF given
211DateTimeImmutable::createFromTimestamp(INF): DateRangeError: DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, INF given
212DateTime::createFromTimestamp(-INF): DateRangeError: DateTime::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, -INF given
213DateTimeImmutable::createFromTimestamp(-INF): DateRangeError: DateTimeImmutable::createFromTimestamp(): Argument #1 ($timestamp) must be a finite number between %i and %i.999999, -INF given
214MyDateTime::createFromTimestamp(0): object(MyDateTime)#%d (3) {
215  ["date"]=>
216  string(26) "1970-01-01 00:00:00.000000"
217  ["timezone_type"]=>
218  int(1)
219  ["timezone"]=>
220  string(6) "+00:00"
221}
222MyDateTimeImmutable::createFromTimestamp(0): object(MyDateTimeImmutable)#%d (3) {
223  ["date"]=>
224  string(26) "1970-01-01 00:00:00.000000"
225  ["timezone_type"]=>
226  int(1)
227  ["timezone"]=>
228  string(6) "+00:00"
229}
230