setlocale(LC_TIME, 'en_US'); echo strftime('In (American) English: %c<br />');
Generally, the language of the system is automatically used. However, this can be overridden using setlocale()
.
Localizing Dates Using strftime()
<?php setlocale(LC_TIME, 'en_US'); echo strftime('In (American) English: %c<br />'); setlocale(LC_TIME, 'en_gb'); echo strftime('In (British) English: %c<br />'); setlocale(LC_TIME, 'de_DE'); echo strftime('Auf Deutsch: %c<br />'); setlocale(LC_TIME, 'fr_FR'); echo strftime('En Francais: %c'); ?>
The function strftime()
expects a format string (as does date()
) in which it accepts a large number of special symbols. Table contains a full list.
Formatting Symbols for strftime()
Symbol | Description |
---|---|
|
Day of week (abbreviated) |
|
Day of week |
|
Month (abbreviated) |
|
Month |
|
Date and time in standard format |
|
Century |
|
Day of month (from |
|
Date in abbreviated format ( |
|
Day of month as a two-character string (from |
|
Year according to the week number, two digits |
|
Year according to the week number, four digits |
|
Hour (from |
|
Hour (from |
|
Day of year (from |
|
Month (from |
|
Minute (from |
|
Newline ( |
|
|
|
Time using a.m./p.m. notation |
|
Time using 24 hours notation |
|
Second (from |
|
Tab ( |
|
Time in |
|
Day of week (from |
|
Week number (Rule: The first Sunday is the first day of the first week.) |
|
Week number (Rule: The first week in the year with at least four days counts as week number 1.) |
|
Day of week (from |
|
Week number (Rule: The first Monday is the first day of the first week.) |
|
Date in standard format (without the time) |
|
Time in standard format (without the date) |
|
Year (two digits) |
|
Year (four digits) |
|
Time zone |
Whenever it says standard format in table, the formatting symbol gets replaced by the associated value according to the local setting. The preceding code changes the locale several times using setlocale()
and then calls strftime()
. Note the differences that can be seen in figure. Also take a look at figure, in which the same script was executed on a Windows machine. According to the documentation, most of strftime()
also works on Windows, but on some configurations changing the locale just does not seem to work. Therefore, it is very important to test first whether the system supports localized dates.