xref: /PHP-8.1/ext/date/tests/bug54851.phpt (revision 109cc0bf)
1--TEST--
2Bug #54851 (DateTime::createFromFormat() doesn't interpret "D")
3--INI--
4date.timezone=UTC
5--FILE--
6<?php
7$date = new DateTime("2011-05-17T22:14:12");
8$date2 = DateTime::createFromFormat("D H i s", $date->format("D"). ' 0 00 00');
9echo $date->format("r"), "\n";
10echo $date2->format("r"), "\n";
11var_dump($date->format("D") == $date2->format("D"));
12
13// Verify that our implementation works regardless of position
14$datePre = DateTime::createFromFormat("!D d M Y", "Fri 17 may 2011");
15$datePost = DateTime::createFromFormat("!d M Y D", "17 may 2011 Fri");
16echo $datePre->format("r"), "\n";
17echo $datePost->format("r"), "\n";
18var_dump($datePre->format("Y-m-d") == $datePost->format("Y-m-d"));
19
20// Verify that our implementation is the same as for the constructor and
21// strtotime
22$date1 = new DateTime("Tuesday");
23$date2 = DateTime::createFromFormat("D H i s", "Tuesday 0 00 00");
24echo $date1->format('r'), "\n";
25echo $date2->format('r'), "\n";
26var_dump($date1->format('D') == $date2->format('D'));
27
28// - when the day is not the same as the day on the original date:
29$date1 = DateTime::createFromFormat("!D d M Y", "Fri 19 November 2011");
30$date2 = new DateTime("Fri 19 November 2011");
31echo $date1->format('r'), "\n";
32echo $date2->format('r'), "\n";
33var_dump($date1->format('Y-m-d') == $date2->format('Y-m-d'));
34
35// - when the day *is* the same as the day on the original date:
36$date1 = DateTime::createFromFormat("!D d M Y", "Sat 19 November 2011");
37$date2 = new DateTime("Sat 19 November 2011");
38echo $date1->format('r'), "\n";
39echo $date2->format('r'), "\n";
40var_dump($date1->format('Y-m-d') == $date2->format('Y-m-d'));
41?>
42--EXPECTF--
43Tue, 17 May 2011 22:14:12 +0000
44Tue, %d %s %d 00:00:00 +0000
45bool(true)
46Fri, 20 May 2011 00:00:00 +0000
47Fri, 20 May 2011 00:00:00 +0000
48bool(true)
49Tue, %d %s %d 00:00:00 +0000
50Tue, %d %s %d 00:00:00 +0000
51bool(true)
52Fri, 25 Nov 2011 00:00:00 +0000
53Fri, 25 Nov 2011 00:00:00 +0000
54bool(true)
55Sat, 19 Nov 2011 00:00:00 +0000
56Sat, 19 Nov 2011 00:00:00 +0000
57bool(true)
58