1db926724SAndreas Gohr<?php 2db926724SAndreas Gohr 3db926724SAndreas Gohrnamespace PHP81_BC\strftime; 4db926724SAndreas Gohr 5db926724SAndreas Gohruse DateTimeInterface; 6db926724SAndreas Gohruse IntlDateFormatter; 7db926724SAndreas Gohruse IntlGregorianCalendar; 8db926724SAndreas Gohr 9db926724SAndreas Gohr/** 10db926724SAndreas Gohr * This formatter uses the IntlDateFormatter class to do proper, locale aware formatting 11db926724SAndreas Gohr */ 12db926724SAndreas Gohrclass IntlLocaleFormatter extends AbstractLocaleFormatter 13db926724SAndreas Gohr{ 14db926724SAndreas Gohr 15db926724SAndreas Gohr /** @var string[] strftime to ICU placeholders */ 16db926724SAndreas Gohr protected $formats = [ 17*4739030dSAndreas Gohr '%a' => 'ccc', // An abbreviated textual representation of the day Sun through Sat 18db926724SAndreas Gohr '%A' => 'EEEE', // A full textual representation of the day Sunday through Saturday 19*4739030dSAndreas Gohr '%b' => 'LLL', // Abbreviated month name, based on the locale Jan through Dec 20db926724SAndreas Gohr '%B' => 'MMMM', // Full month name, based on the locale January through December 21db926724SAndreas Gohr '%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec 22db926724SAndreas Gohr ]; 23db926724SAndreas Gohr 24db926724SAndreas Gohr /** @inheritdoc */ 25db926724SAndreas Gohr public function __invoke(DateTimeInterface $timestamp, string $format) 26db926724SAndreas Gohr { 27db926724SAndreas Gohr $tz = $timestamp->getTimezone(); 28db926724SAndreas Gohr $date_type = IntlDateFormatter::FULL; 29db926724SAndreas Gohr $time_type = IntlDateFormatter::FULL; 30db926724SAndreas Gohr $pattern = ''; 31db926724SAndreas Gohr 32db926724SAndreas Gohr switch ($format) { 33db926724SAndreas Gohr // %c = Preferred date and time stamp based on locale 34db926724SAndreas Gohr // Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM 35db926724SAndreas Gohr case '%c': 36db926724SAndreas Gohr $date_type = IntlDateFormatter::LONG; 37db926724SAndreas Gohr $time_type = IntlDateFormatter::SHORT; 38db926724SAndreas Gohr break; 39db926724SAndreas Gohr 40db926724SAndreas Gohr // %x = Preferred date representation based on locale, without the time 41db926724SAndreas Gohr // Example: 02/05/09 for February 5, 2009 42db926724SAndreas Gohr case '%x': 43db926724SAndreas Gohr $date_type = IntlDateFormatter::SHORT; 44db926724SAndreas Gohr $time_type = IntlDateFormatter::NONE; 45db926724SAndreas Gohr break; 46db926724SAndreas Gohr 47db926724SAndreas Gohr // Localized time format 48db926724SAndreas Gohr case '%X': 49db926724SAndreas Gohr $date_type = IntlDateFormatter::NONE; 50db926724SAndreas Gohr $time_type = IntlDateFormatter::MEDIUM; 51db926724SAndreas Gohr break; 52db926724SAndreas Gohr 53db926724SAndreas Gohr default: 54db926724SAndreas Gohr if (!isset($this->formats[$format])) { 55db926724SAndreas Gohr throw new \RuntimeException("'$format' is not a supported locale placeholder"); 56db926724SAndreas Gohr } 57db926724SAndreas Gohr $pattern = $this->formats[$format]; 58db926724SAndreas Gohr } 59db926724SAndreas Gohr 60db926724SAndreas Gohr // In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and 61db926724SAndreas Gohr // the 4th October was followed by the 15th October. 62db926724SAndreas Gohr // ICU (including IntlDateFormattter) interprets and formats dates based on this cutover. 63db926724SAndreas Gohr // Posix (including strftime) and timelib (including DateTimeImmutable) instead use 64db926724SAndreas Gohr // a "proleptic Gregorian calendar" - they pretend the Gregorian calendar has existed forever. 65db926724SAndreas Gohr // This leads to the same instants in time, as expressed in Unix time, having different representations 66db926724SAndreas Gohr // in formatted strings. 67db926724SAndreas Gohr // To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past. 68db926724SAndreas Gohr $calendar = IntlGregorianCalendar::createInstance(); 69db926724SAndreas Gohr // NOTE: IntlGregorianCalendar::createInstance DOES NOT return an IntlGregorianCalendar instance when 70db926724SAndreas Gohr // using a non-Gregorian locale (e.g. fa_IR)! In that case, setGregorianChange will not exist. 71db926724SAndreas Gohr if (method_exists($calendar, 'setGregorianChange')) $calendar->setGregorianChange(PHP_INT_MIN); 72db926724SAndreas Gohr 73db926724SAndreas Gohr return (new IntlDateFormatter($this->locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); 74db926724SAndreas Gohr } 75db926724SAndreas Gohr 76db926724SAndreas Gohr} 77