Categories
PHP

Localizing Dates

How to localize dates manually or with strftime() function.

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()

SymbolDescription
%aDay of week (abbreviated)
%ADay of week
%b or %hMonth (abbreviated)
%BMonth
%cDate and time in standard format
%CCentury
%dDay of month (from 01 to 31)
%DDate in abbreviated format (mm/dd/yy)
%eDay of month as a two-character string (from ' 1' to '31')
%gYear according to the week number, two digits
%GYear according to the week number, four digits
%HHour (from 00 to 23)
%IHour (from 01 to 12)
%jDay of year (from 001 to 366)
%mMonth (from 01 to 12)
%MMinute (from 00 to 59)
%nNewline (\n)
%pam or pm (or local equivalent)
%rTime using a.m./p.m. notation
%RTime using 24 hours notation
%SSecond (from 00 to 59)
%tTab (\t)
%TTime in hh:ss:mm format
%uDay of week (from 1Mondayto 7Sunday)
%UWeek number (Rule: The first Sunday is the first day of the first week.)
%VWeek number (Rule: The first week in the year with at least four days counts as week number 1.)
%wDay of week (from 0Sunday to 6Saturday)
%WWeek number (Rule: The first Monday is the first day of the first week.)
%xDate in standard format (without the time)
%XTime in standard format (without the date)
%yYear (two digits)
%YYear (four digits)
%z or %ZTime 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: