Skip to content

Commit

Permalink
Merge pull request #16 from asmecher/fix-nongregorian
Browse files Browse the repository at this point in the history
#15 Fix fatal error with non-Gregorian locales
  • Loading branch information
alphp committed Dec 18, 2023
2 parents 51a4ef7 + 8b72060 commit f5e1ece
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/php-8.1-strftime.php
Expand Up @@ -95,7 +95,11 @@ 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 ($calendar instanceof IntlGregorianCalendar) {
$calendar->setGregorianChange(PHP_INT_MIN);
}

return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp);
};
Expand Down

0 comments on commit f5e1ece

Please sign in to comment.