Last active
July 5, 2025 20:16
-
-
Save stemar/36c7ffa31a6167945d547ae6cc89dc0e to your computer and use it in GitHub Desktop.
Localize datetime using encapsulated IntlDateFormatter functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Localize datetime using encapsulated IntlDateFormatter functions | |
| * | |
| * @param IntlCalendar|DateTimeInterface|array|string|int|float $datetime | |
| * @param array $args | |
| * @link https://www.php.net/manual/en/intldateformatter.format.php | |
| * @link https://www.php.net/manual/en/intldateformatter.create.php | |
| * @link https://www.php.net/manual/en/class.locale.php | |
| * @link https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table | |
| * @return string|false | |
| */ | |
| function format_datetime($datetime, array $args = []) { | |
| $args += [ | |
| 'date_type' => IntlDateFormatter::SHORT, | |
| 'time_type' => IntlDateFormatter::SHORT, | |
| 'locale' => substr(Locale::getDefault(), 0, 5) ?: NULL, | |
| 'pattern' => NULL, | |
| 'timezone' => NULL, | |
| ]; | |
| extract($args); | |
| $formatter = datefmt_create($locale, $date_type, $time_type, $timezone, NULL, $pattern); | |
| return datefmt_format($formatter, $datetime); | |
| } | |
| function format_date($datetime, array $args = []) { | |
| $args += [ | |
| 'date_type' => IntlDateFormatter::SHORT, | |
| 'time_type' => IntlDateFormatter::NONE, | |
| ]; | |
| return format_datetime($datetime, $args); | |
| } | |
| function format_time($datetime, array $args = []) { | |
| $args += [ | |
| 'date_type' => IntlDateFormatter::NONE, | |
| 'time_type' => IntlDateFormatter::SHORT, | |
| ]; | |
| return format_datetime($datetime, $args); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
datefmt_create() arguments
locale argument
If
NULL, it will lookup its default value in this order:ini_get('intl.default_locale')setlocale(LC_ALL, 0);$LANGvalue from the set locale of the operating system.date type argument
If
NULL, it will selectIntlDateFormatter::FULL.IntlDateFormatter::NONEto skip the output of the dateIntlDateFormatter::FULLwith weekdayIntlDateFormatter::LONGIntlDateFormatter::MEDIUMIntlDateFormatter::SHORTtime type argument
If
NULL, it will selectIntlDateFormatter::FULL.IntlDateFormatter::NONEto skip the output of the timeIntlDateFormatter::FULLwith time zone descriptionIntlDateFormatter::LONGIntlDateFormatter::MEDIUMIntlDateFormatter::SHORTtime zone argument
If
NULL, it will lookup its default value in this order:date_default_timezone_get()ini_get('date.timezone')'UTC'calendar argument
If
NULL, it will selectIntlDateFormatter::GREGORIAN.pattern argument
If not
NULL, it will override date type and time type arguments.If
NULL, it will defer to date type and time type arguments.