Tuesday, April 7, 2009

PHP format days, month in your language

How do you display day and month from a date in your language (other than English)

Eg: 14/03/2009 in English would be: Saturday 14 March 2009. So how do you get "Saturday" and "March" in your language. Easy. Using setlocale function.

Here is a simple example, displaying day and month in Romanian language:

setlocale(LC_ALL, 'ro-RO');
echo iconv('ISO-8859-1', 'UTF-8',strftime("%A, %d %B %Y",strtotime('2009/03/14')));

so strftime is using setlocale to display date and time. inconv function is converting all the weird characters from to UTF-8 so they are displayed properly.

So now you can easily display dates (days and months) in your native language if is other than English, just setlocale to your language and display through strftime.

No comments: