Home >Backend Development >PHP Tutorial >How to Format a DateTime Object in German Using PHP\'s Intl Extension?
Formatting DateTime Object with Respect to Locale
You want to format a DateTime object while considering the locale setting, particularly for German translation. Using the format() method along with Locale::getDefault() doesn't provide the desired localized output.
The Intl extension offers a more suitable solution for locale-aware date formatting. By utilizing the IntlDateFormatter class, you can specify custom patterns to achieve your formatting requirements.
For your specific scenario, a code snippet that incorporates a custom pattern for German translation would resemble:
$dt = new DateTime; $formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); $formatter->setPattern('E d.M.yyyy'); echo $formatter->format($dt);
This code initializes a DateTime object and creates an IntlDateFormatter with the desired locale, format type, and pattern. The pattern specifies the format of the output date, including the abbreviated weekday (Di. for Dienstag) and the desired date format.
When executed, this code should produce a localized output for the current date in German, similar to:
Di. 4.6.2013
The above is the detailed content of How to Format a DateTime Object in German Using PHP\'s Intl Extension?. For more information, please follow other related articles on the PHP Chinese website!