The setlocale() function is an inbuilt function in PHP which is used to set locale information. Locale setting means assigning your system a geographical location and then perform certain functions based on the locale of the place. Usually, programs dealing with the date and time of other places deal with this.
setlocale( $category , $locale )
Return values: It returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.
Parameter: This functuon accept two parameters as mentioned above and described below:
- Category: It is an integer named constant specifying the category of the functions affected by the locale setting:
- LC_ALL – for all of the below
- LC_COLLATE– for string comparison
- LC_CTYPE– for character classification and conversion
- LC_MONETARY– for localeconv()
- LC_NUMERIC– for decimal separator
- LC_TIME– for date and time formatting with strftime()
- LC_MESSAGES -for system responses
- Locale: Usually an array of required locales for the specified regions.
- 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- the locale setting is not affected, only the current setting is returned.
Below examples illustrate the setlocale() function in PHP:
Example 1: A simple program to generate the locale defined time.
The current german time is 08:17:45 AM and the current english time is 08:17:45 AM
Example 2: Program to check which locale name for german is supported by the system.
Preferred locale for german on this system is 'German_Germany.1252'
Example 3: Simple program to use LC_MONETARY
Array ( [decimal_point] => . [thousands_sep] => [int_curr_symbol] => [currency_symbol] => [mon_decimal_point] => [mon_thousands_sep] => [positive_sign] => [negative_sign] => [int_frac_digits] => 127 [frac_digits] => 127 [p_cs_precedes] => 127 [p_sep_by_space] => 127 [n_cs_precedes] => 127 [n_sep_by_space] => 127 [p_sign_posn] => 127 [n_sign_posn] => 127 [grouping] => Array ( ) [mon_grouping] => Array ( ) )