From a95985070f95f996a76fa05e5ea6d738cb76ec40 Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Mon, 18 Dec 2023 08:13:47 -0800 Subject: [PATCH 1/2] #15 Fix fatal error with non-Gregorian locales --- src/php-8.1-strftime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 1c7ee37..ebb3f70 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -95,7 +95,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : // in formatted strings. // To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past. $calendar = IntlGregorianCalendar::createInstance(); - $calendar->setGregorianChange(PHP_INT_MIN); + // NOTE: IntlGregorianCalendar::createInstance DOES NOT return an IntlGregorianCalendar instance when + // using a non-Gregorian locale (e.g. fa_IR)! In that case, setGregorianChange will not exist. + if (method_exists($calendar, 'setGregorianChange')) $calendar->setGregorianChange(PHP_INT_MIN); return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); }; From 8b7206011c36b74c5b39cf9f4f2af89e91226a2a Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Mon, 18 Dec 2023 08:40:48 -0800 Subject: [PATCH 2/2] Code review tweaks --- src/php-8.1-strftime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index ebb3f70..4f8f916 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -97,7 +97,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : $calendar = IntlGregorianCalendar::createInstance(); // NOTE: IntlGregorianCalendar::createInstance DOES NOT return an IntlGregorianCalendar instance when // using a non-Gregorian locale (e.g. fa_IR)! In that case, setGregorianChange will not exist. - if (method_exists($calendar, 'setGregorianChange')) $calendar->setGregorianChange(PHP_INT_MIN); + if ($calendar instanceof IntlGregorianCalendar) { + $calendar->setGregorianChange(PHP_INT_MIN); + } return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); };