Search code examples
phpdatetime-format

Formatting DateTime object, respecting Locale::getDefault()


I have a DateTime object which I'm currently formating via

$mytime->format("D d.m.Y")

Which gives me exactly the format I need:

Tue 5.3.2012

The only missing point is the correct language. I need German translation of Tue (Tuesday), which is Die (Dienstag).

This gives me the right locale setting

Locale::getDefault()

But I don't know how to tell DateTime::format to use it.

Isn't there a way to do something like:

$mytime->format("D d.m.Y", \Locale::getDefault());

Solution

  • 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())