Skip to content

Instantly share code, notes, and snippets.

@bryanburgers
Last active January 27, 2021 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryanburgers/f375ea3086a0ed029636 to your computer and use it in GitHub Desktop.
Save bryanburgers/f375ea3086a0ed029636 to your computer and use it in GitHub Desktop.
Forcing four-digit years on IntlDateFormatter::SHORT
<?php
$d = new DateTime("2012-10-11");
function unalteredFormat($date, $locale) {
$fmt = new IntlDateFormatter(
$locale,
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE);
$fmtted = $fmt->format($date);
echo "$locale: $fmtted\n";
}
function modifiedFormat($date, $locale) {
$fmt = new IntlDateFormatter(
$locale,
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE);
$pattern = $fmt->getPattern();
$pattern = preg_replace(
'/(?<!y)yy(?!y)/',
'yyyy',
$pattern);
$fmt->setPattern($pattern);
$fmtted = $fmt->format($date);
echo "$locale: $fmtted\n";
}
$locales = array(
'en_US',
'en_GB',
'de_DE',
'fr_FR',
'ru_RU',
'en_ZW',
'ps_AF',
);
echo "-- Unaltered format --\n";
foreach ($locales as $locale) {
unalteredFormat($d, $locale);
}
echo "\n";
echo "-- Modified format --\n";
foreach ($locales as $locale) {
modifiedFormat($d, $locale);
}
-- Unaltered format --
en_US: 10/11/12
en_GB: 11/10/2012
de_DE: 11.10.12
fr_FR: 11/10/2012
ru_RU: 11.10.12
en_ZW: 11/10/2012
ps_AF: م. ۲۰۱۲/۱۰/۱۱
-- Modified format --
en_US: 10/11/2012
en_GB: 11/10/2012
de_DE: 11.10.2012
fr_FR: 11/10/2012
ru_RU: 11.10.2012
en_ZW: 11/10/2012
ps_AF: م. ۲۰۱۲/۱۰/۱۱
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment