Skip to content

Commit

Permalink
Format: apply a fallback pattern to dateIntlReadable to prevent excep…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
SKuipers committed May 17, 2024
1 parent 2051b26 commit 1cf75e0
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Services/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,27 @@ 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
* @param string $pattern (Optional) The pattern string for Unicode formatting supported by
* IntlDateFormatter::setPattern().
*
* See: https://unicode-org.github.io/icu/userguide/format_parse/datetime/
* Default: 'MMM d, Y'
* @param string $fallbackPattern (Optional) A fallback pattern string to use for the date using
* PHP's DateTime library if intl is not available.
* Default: 'M d, Y'
*
* @return string The formatted date string.
*/
public static function dateIntlReadable($dateString, $pattern = 'MMM d, yyyy'): string
public static function dateIntlReadable($dateString, $pattern = 'MMM d, yyyy', $fallbackPattern = 'M d, Y'): string
{
if (empty($dateString)) {
return '';
}

if (!static::$intlFormatterAvailable) {
throw new \Exception('IntlDateFormatter not available.');
return static::date($dateString, $fallbackPattern);
}

$formatter = new \IntlDateFormatter(
static::$settings['code'],
\IntlDateFormatter::FULL,
Expand Down Expand Up @@ -184,7 +189,7 @@ public static function dateReadable($dateString, $format = '%b %e, %Y')
*
* @return string The formatted date string.
*/
public static function dateTimeIntlReadable($dateString, $pattern = 'MMM d, yyyy HH:mm'): string
public static function dateTimeIntlReadable($dateString, $pattern = 'MMM d, yyyy HH:mm', $fallbackPattern = 'M d, Y H:i:s'): string
{
return static::dateIntlReadable($dateString, $pattern);
}
Expand Down Expand Up @@ -326,7 +331,7 @@ public static function relativeTime($dateString, $tooltip = true, $relativeStrin
break;
default:
$timeDifference = 0;
$time = static::dateReadable($dateString);
$time = static::dateIntlReadable($dateString);
}

if ($relativeString && $timeDifference > 0) {
Expand Down

0 comments on commit 1cf75e0

Please sign in to comment.