About a week ago i was working on a twitter widget for a website. This required some dates to be displayed in Dutch. And i found out the hard way my knowledge on this has faded away over time.
So the code i was working on. Did something like this.
$date = date('D M d H:i:s Y', strtotime($someVar));
My thought was that by setting the correct locale the dates would appear in the correct language. Wrong!
date_default_timezone_set('Europe/Amsterdam');
setlocale(LC_ALL, 'nl_NL.utf8');
After a reload i was greeted by the same dates as before. In plain English. Oke no worries. Let’s see what setlocale returns.
var_dump(setlocale(LC_ALL, 'nl_NL.utf8'));
bool(false)
That’s not good. Seems like we are missing some locales on the server. Let’s check.
locale -a
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
…
And some more output after that. But not the one i am looking for. But thankfully aptitude was kind enough to provide the missing language packages.
nl_NL
nl_NL@euro
nl_NL.iso88591
nl_NL.iso885915@euro
nl_NL.utf8
So let’s set the correct locale for this script.
setlocale(LC_ALL, 'nl_NL.utf8');
But still no changes. I must be missing something….. Let’s consult the manual. The last line in the examples section is what i was looking for
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
Duuh! Completely forgot about strftime. Let’s change the code.
strftime('%a %b %d %H:%M:%S %Y', strtotime($somevar));
ma aug 15 14:55:06 2011
Perfect. That did it.
Twitter
I am Thijs Lensselink a Webdeveloper from the Netherlands.