Last active
April 4, 2023 00:44
-
-
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 $kwargs | |
* @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 | |
* @return string|false | |
*/ | |
function datetime_format($datetime, array $kwargs = []) { | |
extract($kwargs + [ | |
'date_type' => IntlDateFormatter::SHORT, | |
'time_type' => IntlDateFormatter::SHORT, | |
'locale' => substr(Locale::getDefault(), 0, 5) ?: NULL, | |
'pattern' => NULL, | |
'timezone' => NULL, | |
]); | |
$fmt = datefmt_create($locale, $date_type, $time_type, $timezone, NULL, $pattern); | |
return datefmt_format($fmt, $datetime); | |
} |
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);
$LANG
value from the set locale of the operating system.date type argument
If
NULL
, it will selectIntlDateFormatter::FULL
.IntlDateFormatter::NONE
to skip the output of the dateIntlDateFormatter::FULL
with weekdayIntlDateFormatter::LONG
IntlDateFormatter::MEDIUM
IntlDateFormatter::SHORT
time type argument
If
NULL
, it will selectIntlDateFormatter::FULL
.IntlDateFormatter::NONE
to skip the output of the timeIntlDateFormatter::FULL
with time zone descriptionIntlDateFormatter::LONG
IntlDateFormatter::MEDIUM
IntlDateFormatter::SHORT
time 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.