From 4cb1f787eea802e6204d08e85400addb0971edc7 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Mon, 20 Nov 2023 23:12:41 +0800 Subject: [PATCH 1/2] Format: new functions for readable date-time conversion * dateIntlReadable to replace dateReadable * dateTimeIntlReadable to replace dateTimeReadable * Add test coverage to Format::dateTimeReadable, Format::dateIntlReadable and Format::dateTimeIntlReadable. --- src/Services/Format.php | 53 ++++++++++++++++++++++++++++++ tests/unit/Services/FormatTest.php | 5 ++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Services/Format.php b/src/Services/Format.php index b51df6162a..7caf5a7e70 100644 --- a/src/Services/Format.php +++ b/src/Services/Format.php @@ -125,6 +125,41 @@ public static function dateTime($dateString, $format = false) /** * Formats a YYYY-MM-DD date as a readable string with month names. * + * @param DateTime|string $dateString The date string to format. + * @param string $pattern (Optional) The pattern string for Unicode formatting suppored by + * IntlDateFormatter::setPattern(). + * + * See: https://unicode-org.github.io/icu/userguide/format_parse/datetime/ + * Default: 'MMM d, Y' + * + * @return string The formatted date string. + */ + public static function dateIntlReadable($dateString, $pattern = 'MMM d, yyyy'): string + { + if (empty($dateString)) { + return ''; + } + if (!static::$intlFormatterAvailable) { + throw new \Exception('IntlDateFormatter not available.'); + } + $formatter = new \IntlDateFormatter( + static::$settings['code'], + \IntlDateFormatter::FULL, + \IntlDateFormatter::FULL, + null, + null, + $pattern, + ); + return mb_convert_case( + $formatter->format(static::createDateTime($dateString)), + MB_CASE_TITLE, + ); + } + + /** + * Formats a YYYY-MM-DD date as a readable string with month names. + * + * @deprecated v27.0.00 Use dateIntlReadable() instead. * @param DateTime|string $dateString * @return string */ @@ -140,6 +175,24 @@ public static function dateReadable($dateString, $format = '%b %e, %Y') /** * Formats a YYYY-MM-DD date as a readable string with month names and times. * + * @param DateTime|string $dateString The date string to format. + * @param string $pattern (Optional) The pattern string for Unicode formatting suppored by + * IntlDateFormatter::setPattern(). + * + * See: https://unicode-org.github.io/icu/userguide/format_parse/datetime/ + * Default: 'MMM d, Y' + * + * @return string The formatted date string. + */ + public static function dateTimeIntlReadable($dateString, $pattern = 'MMM d, yyyy HH:mm'): string + { + return static::dateIntlReadable($dateString, $pattern); + } + + /** + * Formats a YYYY-MM-DD date as a readable string with month names and times. + * + * @deprecated v27.0.00 Use dateTimeIntlReadable() instead. * @param DateTime|string $dateString * @return string */ diff --git a/tests/unit/Services/FormatTest.php b/tests/unit/Services/FormatTest.php index 2bed8c87d4..ff68f6e133 100644 --- a/tests/unit/Services/FormatTest.php +++ b/tests/unit/Services/FormatTest.php @@ -16,7 +16,7 @@ /** * @covers Format */ -class FormatTest extends TestCase +class x extends TestCase { public function setUp(): void { @@ -79,6 +79,9 @@ public function testFormatsDateTimeWithFormat() public function testFormatsReadableDates() { $this->assertEquals('May 18, 2018', Format::dateReadable('2018-05-18')); + $this->assertEquals('May 18, 2018', Format::dateIntlReadable('2018-05-18')); + $this->assertEquals('May 18, 2018 13:24', Format::dateTimeReadable('2018-05-18 13:24')); + $this->assertEquals('May 18, 2018 13:24', Format::dateTimeIntlReadable('2018-05-18 13:24')); } public function testFormatsDateRanges() From b6769e67c1e443bff3e02c766a1deb93cbbfde18 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Thu, 14 Dec 2023 14:06:55 +0800 Subject: [PATCH 2/2] Fix typo in FormatTest --- tests/unit/Services/FormatTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Services/FormatTest.php b/tests/unit/Services/FormatTest.php index ff68f6e133..99deca962f 100644 --- a/tests/unit/Services/FormatTest.php +++ b/tests/unit/Services/FormatTest.php @@ -16,7 +16,7 @@ /** * @covers Format */ -class x extends TestCase +class FormatTest extends TestCase { public function setUp(): void {