PHP 8.1 – Localized Date Formats

PHP 8.1 – Localized Date Formats

flags on the sky, usa

Format your date with local date structure to combine different format and pattern for full date via new class ` IntlDatePatternGenerator and methods like: getBestPattern() .

New class interface:

class IntlDatePatternGenerator
{
    public function __construct(?string $locale = null) {}
 
    public static function create(?string $locale = null): ?IntlDatePatternGenerator {}
 
    public function getBestPattern(string $skeleton): string|false {}
}
 
// Procedural style:
function datepatterngenerator_create(?string $locale = null): ?IntlDatePatternGenerator {}
 
function datepatterngenerator_get_best_pattern(IntlDatePatternGenerator $patternGenerator, string $skeleton): string|false {}

Example usage:

$skeleton = "YYYYMMdd";
 
$today = \DateTimeImmutable::createFromFormat('Y-m-d', '2021-04-24');
 
$dtpg = new \IntlDatePatternGenerator("de_DE");
$pattern = $dtpg->getBestPattern($skeleton);
echo "de: ", \IntlDateFormatter::formatObject($today, $pattern, "de_DE"), "\n";
 
$dtpg = new \IntlDatePatternGenerator("en_US");
$pattern = $dtpg->getBestPattern($skeleton), "\n";
echo "en: ", \IntlDateFormatter::formatObject($today, $pattern, "en_US"), "\n";
 
/*
de: 24.04.2021
en: 04/24/2021
*/

More about PHP 8.1

Read about all PHP 8.1 features and changes in here.