Skip to content

Commit 612896a

Browse files
Merge branch '5.2' into 5.3
* 5.2: [Form] fix support for years outside of the 32b range on x86 arch Add an upgrade note about the removal of Serializable
2 parents 2ede829 + 4bc95a5 commit 612896a

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

DateFormatter/IntlDateFormatter.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static function create(?string $locale, ?int $datetype, ?int $timetype, $
182182
/**
183183
* Format the date/time value (timestamp) as a string.
184184
*
185-
* @param int|\DateTimeInterface $timestamp The timestamp to format
185+
* @param int|string|\DateTimeInterface $timestamp The timestamp to format
186186
*
187187
* @return string|bool The formatted value or false if formatting failed
188188
*
@@ -194,11 +194,15 @@ public function format($timestamp)
194194
{
195195
// intl allows timestamps to be passed as arrays - we don't
196196
if (\is_array($timestamp)) {
197-
$message = 'Only integer Unix timestamps and DateTime objects are supported';
197+
$message = 'Only Unix timestamps and DateTime objects are supported';
198198

199199
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message);
200200
}
201201

202+
if (\is_string($timestamp) && $dt = \DateTime::createFromFormat('U', $timestamp)) {
203+
$timestamp = $dt;
204+
}
205+
202206
// behave like the intl extension
203207
$argumentError = null;
204208
if (!\is_int($timestamp) && !$timestamp instanceof \DateTimeInterface) {
@@ -214,7 +218,7 @@ public function format($timestamp)
214218
}
215219

216220
if ($timestamp instanceof \DateTimeInterface) {
217-
$timestamp = $timestamp->getTimestamp();
221+
$timestamp = $timestamp->format('U');
218222
}
219223

220224
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
@@ -585,8 +589,7 @@ public function setTimeZone($timeZone)
585589
*/
586590
protected function createDateTime(int $timestamp)
587591
{
588-
$dateTime = new \DateTime();
589-
$dateTime->setTimestamp($timestamp);
592+
$dateTime = \DateTime::createFromFormat('U', $timestamp);
590593
$dateTime->setTimezone($this->dateTimeZone);
591594

592595
return $dateTime;

Tests/DateFormatter/IntlDateFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testFormatWithUnsupportedTimestampArgument()
7575
} catch (\Exception $e) {
7676
$this->assertInstanceOf(MethodArgumentValueNotImplementedException::class, $e);
7777

78-
$this->assertStringEndsWith('Only integer Unix timestamps and DateTime objects are supported. Please install the "intl" extension for full localization capabilities.', $e->getMessage());
78+
$this->assertStringEndsWith('Only Unix timestamps and DateTime objects are supported. Please install the "intl" extension for full localization capabilities.', $e->getMessage());
7979
}
8080
}
8181

0 commit comments

Comments
 (0)