To format a local time/date according to locale settings, like month and weekday names and other language dependent strings, we can use
strftime() function respect the current locale set with
setlocale().
Example On Linux, you have to use xx_XX format for locale country code
<?php
echo '<pre>' . "\n";
setlocale(LC_ALL, 'id_ID');
echo strftime("Today in Indonesia is %A") . "\n";
//And output in UTF-8 charset is easy
setlocale(LC_ALL, 'id_ID.UTF8');
echo strftime("Today in Indonesia is %A") . "\n";
//And will output "March"
//in German language correctly: "März"
setlocale(LC_ALL, 'de_DE.UTF8');
echo strftime("This month in German is %B") . "\n";
echo '</pre>' . "\n";
?>
On Windows you have to use XXX format for locale country code. If it's does not work, try xx_XX format.
<?php
echo '<pre>' . "\n";
setlocale(LC_ALL, 'IND');
echo strftime("Today in Indonesia is %A") . "\n";
//To output in UTF-8, it's not easy
//And to output "March" in German language correctly ("März")
setlocale(LC_ALL, 'DEU');
$str1 = strftime("This month in German is %B");
$str2 = iconv('ISO-8859-1', 'UTF-8', $str1);
echo $str1 . "\n"; //not correct
echo $str2 . "\n"; //correct
echo '</pre>' . "\n";
?>
- Workaround: for Linux, Windows and All Platform
If we develop our PHP code on Windows workstation, it's really pain if we have to change
locale format everytime we upload it to Linux or Nix server--as post production machine. For example, on FreeBSD machine some user report that they have to use the charset to get desired result, ie: setlocale(LC_ALL, 'fr_FR.8859-1') to display correct French language.
Fortunately, we can put as many parameters for
setlocale() function to solve our problem.
Syntax: setlocale(category, param-1 [, param-2, ... param-n])
PHP will use the first available parameter provided by the machine.
<?php
echo '<pre>' . "\n";
//Add english as default (if all Indonesian not available)
setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'ind_IND.UTF8', 'ind_IND.UTF-8', 'ind_IND.8859-1', 'ind_IND', 'IND.UTF8', 'IND.UTF-8', 'IND.8859-1', 'IND', 'Indonesian.UTF8', 'Indonesian.UTF-8', 'Indonesian.8859-1', 'Indonesian', 'Indonesia', 'id', 'ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US.8859-1', 'en_US', 'American', 'English');
//will output something like: Kamis, 17 Agustus 2008
echo strftime("%A, %d %B %Y") . "\n";
echo '</pre>' . "\n";
?>
Links - Locale Codification for Window from MSDN-Microsoft
Using XXX format or ISO-639
This list is specific for Visual Studio or .NET Framework. Altough not all countries listed here, as my country (Indonesia), I successfully change the locale setting to Indonesian in PHP using setlocale(LC_ALL, 'IND'). So you can try the same...