In PHP development, the setlocale() function and the LC_TIME constant are often used to control the localization of date and time display. By setting the appropriate locale identifier, programs can correctly display dates and times according to different language and regional conventions. This article explains in detail how to use setlocale() with LC_TIME to format dates and quickly achieve localized display.
The setlocale() function is used to set the locale information for the current script, which can affect the display of dates, times, numeric formats, and more. This function accepts two parameters:
category: Sets different types of localization information. Common ones include:
LC_ALL: Sets all categories.
LC_TIME: Sets localization for time and date.
LC_NUMERIC: Sets numeric formats.
LC_MONETARY: Sets currency formats.
LC_COLLATE: Sets string sorting rules.
locale: The identifier for the locale. The format is “language_country” or “language_country.encoding.” For example, “zh_CN.UTF-8” represents Simplified Chinese (China, UTF-8 encoding).
After setting the appropriate locale with setlocale(), PHP will use this information to handle related date and time displays.
The LC_TIME constant specifies localization settings for time and date. With it, you can control the date format, month names, day names, and more. To implement localized date formats, setlocale() is typically used together with strftime().
Example:
<span><span><span class="hljs-comment">// Set locale to Simplified Chinese (China)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_TIME, </span><span><span class="hljs-string">'zh_CN.UTF-8'</span></span><span>);
<p></span>// Get the current date and output in localized format<br>
echo strftime('%A, %d %B %Y'); // Output: 星期三, 26 六月 2025<br>
</span>
In this example, setlocale() sets the script’s locale to Simplified Chinese (China). The strftime() function then outputs the date in Chinese format according to the locale settings.
With strftime(), you can flexibly define how dates are displayed. Here are some common format specifiers:
%A: Full weekday name (e.g., Monday, Tuesday)
%a: Abbreviated weekday name (e.g., Mon, Tue)
%B: Full month name (e.g., January, February)
%b: Abbreviated month name (e.g., Jan, Feb)
%d: Day of the month (01–31)
%Y: Four-digit year (e.g., 2025)
%y: Two-digit year (e.g., 25)
%H: Hour in 24-hour format (00–23)
%M: Minutes (00–59)
%S: Seconds (00–59)
Example:
<span><span><span class="hljs-comment">// Set locale to English (United States)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_TIME, </span><span><span class="hljs-string">'en_US.UTF-8'</span></span><span>);
<p></span>// Get the current date and output in localized format<br>
echo strftime('%A, %d %B %Y'); // Output: Wednesday, 26 June 2025<br>
</span>
With different locale settings and format specifiers, you can easily display dates in various languages and regions.
Locale support issues: Not all servers support all locale identifiers. When using setlocale(), it’s best to check available locales on the server with the locale -a command. If the specified locale does not exist, setlocale() will return false, so choose locale identifiers carefully.
Timezone settings: setlocale() only controls localization formats; timezone settings require date_default_timezone_set(). If you need to display time in a specific timezone, use it together with date_default_timezone_set().
<span><span><span class="hljs-comment">// Set timezone</span></span><span>
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span></span><span>(</span><span><span class="hljs-string">'Asia/Shanghai'</span></span><span>);
<p></span>// Set locale to Simplified Chinese (China)<br>
setlocale(LC_TIME, 'zh_CN.UTF-8');</p>
<p>// Output current time<br>
echo strftime('%Y-%m-%d %H:%M:%S'); // Output: 2025-06-26 10:30:45<br>
</span>
Encoding issues: To support Chinese and other non-Latin characters, it is recommended to use UTF-8 encoding. If you encounter garbled text when setting the locale, try installing the corresponding language pack on the server and ensure consistent encoding.
By using setlocale() with the LC_TIME constant, PHP can localize date and time displays. With different locale identifiers, developers can automatically adjust date formats based on users’ locations, providing a more user-friendly experience. Always check if the server supports the required locale identifiers and ensure consistent encoding to avoid display issues such as garbled text.
With the examples and explanations in this article, you should now be able to quickly implement localized date formatting and add multilingual support to your application.