Hướng dẫn
Quảng cáo

Hàm setlocale() trong PHP

Hướng dẫn cách sử dụng hàm setlocale() trong lập trình PHP

Tác dụng của hàm setlocale()

The setlocale() function sets locale information.

The following table summarizes the technical details of this function.

Return Value: Returns the current locale settings, or FALSE on failure. The return value depends on the system that PHP is running.
Version: PHP 4+

Tip: In computing, a locale refers to a set of parameters that defines the user's language, country/region, and any special preference that the user wants to see in their user interface.


Syntax

The basic syntax of the setlocale() function is given with:

setlocale(category, locale);

The following example shows the setlocale() function in action.

<?php
// Set locale
setlocale(LC_ALL, "en_US");

// Get current locale setting
echo setlocale(LC_ALL, 0);
?>

Parameters

The setlocale() function accepts the following parameters.

Parameter Description
category

Required. It is a named constant specifying the category of the functions affected by the locale setting. Available constants are:

  • LC_ALL sets localization rules for all the following categories of functions.
  • LC_COLLATE for string comparison, see strcoll().
  • LC_CTYPE for character classification and conversion, see strtoupper().
  • LC_MONETARY for monetary representation, see localeconv().
  • LC_NUMERIC for numeric representation, see localeconv().
  • LC_TIME for date and time representation, see strftime().
  • LC_MESSAGES for system responses.
locale

Required. Specifies what country/region to set the locale information to. It can be a string or an array. It is also possible to pass multiple locale string.

  • If locale is NULL or the empty string "", the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG".
  • If locale is "0", the locale setting is not affected, only the current setting is returned.
  • If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a locale which is possibly not available on that system.

More Examples

Here're some more examples showing how setlocale() function actually works:

The following example prints date formatted according to the locale setting.

<?php
// Set locale to Dutch
setlocale(LC_ALL, "nl_NL");

// Outputs date according to locale
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 04, 11, 2020));
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements