Formatting DateTime object, respecting Locale::getDefault()

Solution 1:

You can use the Intl extension to format the date. It will format dates/times according to the chosen locale, or you can override that with IntlDateFormatter::setPattern().

A quicky example of using a custom pattern, for your desired output format, might look like.

$dt = new DateTime;

$formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
$formatter->setPattern('E d.M.yyyy');

echo $formatter->format($dt);

Which outputs the following (for today, at least).

Di. 4.6.2013

Solution 2:

That's because format does not pay attention to locale. You should use strftime instead.

For example:

setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())