Localizing Dates
How to localize dates manually or with strftime() function.
Commonly Used Date Formats
'; echo 'UK format: ' . date('d/m/Y') . '
'; echo 'German format: ' . date('d.m.Y') . '
'; echo 'International format: ' . date('Y-d-m');
- In the United States, it’s (mostly) month, day, and year
- In the United Kingdom and the rest of Europe, it’s (mostly) day, month, and year
- The international standard date notation starts with the year and continues with the month and day
The preceding code used a four-digit representation of the year because this is nonambiguous. In practice, however, two-digit years are also commonly used.
Manually Localizing Dates
If you want to localized date and time values manually, you have to do the translations by yourself and store the results in an array. Then, you can use date() to retrieve information about a date. This serves as an index for your array.
Example (Spanish): Localizing Dates Manually
The preceding code does this for both the day of the month and the month itself. One array contains the Spanish weekdays; another one contains the month names.
Note: the array $months has a dummy element at position 0.
Localizing Dates Using strftime()
Note: This function has been DEPRECATED as of PHP 8.1.0, use IntlDateFormatter::format() instead.
The PHP function strftime() formats a date/time value according to the sytem’s locale, for example, to the web server’s local settings.
Generally, the language of the system is automatically used. However, this can be overridden using setlocale() :
'); setlocale(LC_TIME, 'en_gb'); echo strftime('In (British) English: %c
'); setlocale(LC_TIME, 'de_DE'); echo strftime('Auf Deutsch: %c
'); 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 of strftime() special symbols.
The preceding code changes the locale several times using setlocale() and then calls strftime() .
Output of the current date in different locales.
In (American) English: Wed 06 Apr 2022 01:54:06 PM CEST In (British) English: Wed 06 Apr 2022 01:54:06 PM CEST Auf Deutsch: Mit 06 Apr 2022 13:54:06 CEST En Francais: mer 06 avr 2022 13:54:06 CEST
System does not support locales:
In (American) English: 04/06/22 01:54:06 PM CEST In (British) English: 04/06/22 01:54:06 PM CEST Auf Deutsch: 04/06/22 13:54:06 CEST En Francais: 04/06/22 13:54:06 CEST
Note the differences that can be seen in outputs. 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.
Formatting Symbols for strftime()
Whenever it says standard format in the table, the formatting symbol gets replaced by the associated value according to the local setting.
The Date and Time Tutorials:
Localizing Dates
How to localize dates manually or with strftime() function.
Commonly Used Date Formats
'; echo 'UK format: ' . date('d/m/Y') . '
'; echo 'German format: ' . date('d.m.Y') . '
'; echo 'International format: ' . date('Y-d-m');
- In the United States, it’s (mostly) month, day, and year
- In the United Kingdom and the rest of Europe, it’s (mostly) day, month, and year
- The international standard date notation starts with the year and continues with the month and day
The preceding code used a four-digit representation of the year because this is nonambiguous. In practice, however, two-digit years are also commonly used.
Manually Localizing Dates
If you want to localized date and time values manually, you have to do the translations by yourself and store the results in an array. Then, you can use date() to retrieve information about a date. This serves as an index for your array.
Example (Spanish): Localizing Dates Manually
The preceding code does this for both the day of the month and the month itself. One array contains the Spanish weekdays; another one contains the month names.
Note: the array $months has a dummy element at position 0.
Localizing Dates Using strftime()
Note: This function has been DEPRECATED as of PHP 8.1.0, use IntlDateFormatter::format() instead.
The PHP function strftime() formats a date/time value according to the sytem’s locale, for example, to the web server’s local settings.
Generally, the language of the system is automatically used. However, this can be overridden using setlocale() :
'); setlocale(LC_TIME, 'en_gb'); echo strftime('In (British) English: %c
'); setlocale(LC_TIME, 'de_DE'); echo strftime('Auf Deutsch: %c
'); 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 of strftime() special symbols.
The preceding code changes the locale several times using setlocale() and then calls strftime() .
Output of the current date in different locales.
In (American) English: Wed 06 Apr 2022 01:54:06 PM CEST In (British) English: Wed 06 Apr 2022 01:54:06 PM CEST Auf Deutsch: Mit 06 Apr 2022 13:54:06 CEST En Francais: mer 06 avr 2022 13:54:06 CEST
System does not support locales:
In (American) English: 04/06/22 01:54:06 PM CEST In (British) English: 04/06/22 01:54:06 PM CEST Auf Deutsch: 04/06/22 13:54:06 CEST En Francais: 04/06/22 13:54:06 CEST
Note the differences that can be seen in outputs. 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.
Formatting Symbols for strftime()
Whenever it says standard format in the table, the formatting symbol gets replaced by the associated value according to the local setting.
The Date and Time Tutorials: