Skip to content

Commit 58d841d

Browse files
authored
Merge pull request #7500 from magento-trigger/AC-2563
Fix DateTimeFormatter for PHP8.1
2 parents 54e8398 + 2f983a0 commit 58d841d

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/internal/Magento/Framework/Stdlib/DateTime/DateTimeFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ public function formatObject($object, $format = null, $locale = null)
6262
*/
6363
protected function doFormatObject($object, $format = null, $locale = null)
6464
{
65-
$pattern = $dateFormat = $timeFormat = $calendar = null;
65+
$pattern = $calendar = null;
6666

6767
if (is_array($format)) {
6868
list($dateFormat, $timeFormat) = $format;
6969
} elseif (is_numeric($format)) {
7070
$dateFormat = $format;
71-
} elseif (is_string($format) || null == $format) {
71+
$timeFormat = \IntlDateFormatter::FULL;
72+
} elseif (is_string($format) || null === $format) {
7273
$dateFormat = $timeFormat = \IntlDateFormatter::MEDIUM;
7374
$pattern = $format;
7475
} else {

lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/DateTimeFormatterTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,41 @@ public function testFormatObjectIfPassedWrongFormat()
139139
$reflectionProperty->setValue($dateTimeFormatter, $this->localeResolverMock);
140140
$dateTimeFormatter->formatObject(new \DateTime('2013-06-06 17:05:06 Europe/Dublin'), new \StdClass());
141141
}
142+
143+
/**
144+
* @dataProvider formatObjectNumericFormatDataProvider
145+
*/
146+
public function testFormatObjectNumericFormat($format, $expected)
147+
{
148+
/** @var DateTimeFormatter $dateTimeFormatter */
149+
$dateTimeFormatter = $this->objectManager->getObject(
150+
DateTimeFormatter::class,
151+
[
152+
'useIntlFormatObject' => false,
153+
]
154+
);
155+
156+
$reflection = new \ReflectionClass(get_class($dateTimeFormatter));
157+
$reflectionProperty = $reflection->getProperty('localeResolver');
158+
$reflectionProperty->setAccessible(true);
159+
$reflectionProperty->setValue($dateTimeFormatter, $this->localeResolverMock);
160+
$result = $dateTimeFormatter->formatObject(
161+
new \DateTime('2022-03-30 00:01:02 GMT'),
162+
$format,
163+
'en_US'
164+
);
165+
$this->assertEquals($expected, $result);
166+
}
167+
168+
public function formatObjectNumericFormatDataProvider()
169+
{
170+
return [
171+
[null, 'Mar 30, 2022, 12:01:02 AM'],
172+
[\IntlDateFormatter::NONE, '12:01:02 AM Greenwich Mean Time'],
173+
[\IntlDateFormatter::SHORT, '3/30/22, 12:01:02 AM Greenwich Mean Time'],
174+
[\IntlDateFormatter::MEDIUM, 'Mar 30, 2022, 12:01:02 AM Greenwich Mean Time'],
175+
[\IntlDateFormatter::LONG, 'March 30, 2022 at 12:01:02 AM Greenwich Mean Time'],
176+
[\IntlDateFormatter::FULL, 'Wednesday, March 30, 2022 at 12:01:02 AM Greenwich Mean Time'],
177+
];
178+
}
142179
}

0 commit comments

Comments
 (0)