PHP Date function output in Italian

date() is not locale-aware. You should use strftime() and its format specifiers to output locale-aware dates (from the date() PHP manual):

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

Regarding Anti Veeranna's comment: he is absolutely right, since you have to be very careful with setting locales as they are sometimes not limited to the current script scope. The best way would be:

$oldLocale = setlocale(LC_TIME, 'it_IT');
echo utf8_encode( strftime("%a %d %b %Y", $row['eventtime']) );
setlocale(LC_TIME, $oldLocale);

I found that setlocale isn't reliable, as it is set per process, not per thread (the manual mentions this). This means other running scripts can change the locale at any time. A solution is using IntlDateFormatter from the intl php extension.

$fmt = new \IntlDateFormatter('it_IT', NULL, NULL);
$fmt->setPattern('d MMMM yyyy HH:mm'); 
// See: http://userguide.icu-project.org/formatparse/datetime for pattern syntax
echo $fmt->format(new \DateTime()); 

If it doesn't work you might need to:

  • Install intl php extension (ubuntu example): sudo apt-get install php5-intl

  • Install the locale you want to use: sudo locale-gen it_IT


it_IT locale has to be installed/enabled by your server admin, otherwise this will not work.

So, Jonathan's solution is probably the best.

Tags:

Php

Date