- Commonly Used Date Formats
- Manually Localizing Dates
- Automatically Localizing Dates with strftime() and setlocale()
Commonly Used Date Formats
<?php echo 'US format: ' . date('m/d/Y') . '<br>'; echo 'UK format: ' . date('d/m/Y') . '<br>'; echo 'German format: ' . date('d.m.Y') . '<br>'; echo 'International format: ' . date('Y-d-m');
Depending on where you are, the order in which day, month, and year are used might vary:
- 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
<?php $weekdays = array( 'domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado' ); $months = array(null, 'enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre' ); $weekday = $weekdays[ date('w') ]; $month = $months[ date('n') ]; $day = date('j'); $year = date('Y'); echo "$weekday, $day $month $year"; // domingo, 29 enero 2023
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.
<?php setlocale(LC_TIME, 'en_US'); echo strftime('In (American) English: %c');
Generally, the language of the system is automatically used. However, this can be overridden using setlocale()
:
<?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 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()
Symbol | Description |
---|---|
%a | Day of week (abbreviated) |
%A | Day of week |
%b or %h | Month (abbreviated) |
%B | Month |
%c | Date and time in standard format |
%C | Century |
%d | Day of month (from 01 to 31 ) |
%D | Date in abbreviated format (mm/dd/yy ) |
%e | Day of month as a two-character string (from ' 1' to '31' ) |
%g | Year according to the week number, two digits |
%G | Year according to the week number, four digits |
%H | Hour (from 00 to 23 ) |
%I | Hour (from 01 to 12 ) |
%j | Day of year (from 001 to 366 ) |
%m | Month (from 01 to 12 ) |
%M | Minute (from 00 to 59 ) |
%n | Newline (\n ) |
%p | am or pm (or local equivalent) |
%r | Time using a.m./p.m. notation |
%R | Time using 24 hours notation |
%S | Second (from 00 to 59 ) |
%t | Tab (\t ) |
%T | Time in hh:ss:mm format |
%u | Day of week (from 1 Mondayto 7 Sunday) |
%U | Week number (Rule: The first Sunday is the first day of the first week.) |
%V | Week number (Rule: The first week in the year with at least four days counts as week number 1.) |
%w | Day of week (from 0 Sunday to 6 Saturday) |
%W | Week number (Rule: The first Monday is the first day of the first week.) |
%x | Date in standard format (without the time) |
%X | Time in standard format (without the date) |
%y | Year (two digits) |
%Y | Year (four digits) |
%z or %Z | Time zone |
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:
- PHP DateTime Class
- PHP DateTimeZone Class – times in different countries
- PHP DateInterval Class – adds or subtracts dates/times
- PHP DatePeriod Class – generates date or time ranges
- PHP Validating Age and Date of Birth
- Sunset, Sunrise, Transit, and Twilight
- Localizing Dates
- Localizing Dates with IntlDateFormatter Class