PHP Function Reference

PHP setlocale() Function



The PHP setlocale() function sets the locale information, either changing the entire locale or portions of it. Locale information is language, monetary, time and other information specific for a geographical area.

Syntax

setlocale(category, locales)

Parameters

category Required. A 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, see strcoll() function
  • LC_CTYPE - for character classification and conversion, for example strtoupper() function
  • LC_MONETARY - for localeconv() function
  • LC_NUMERIC - for decimal separator (see also localeconv() function)
  • LC_TIME - for date and time formatting with strftime() function
  • LC_MESSAGES - for system responses (available if PHP is compiled with libintl)
locales Required. Specify the country/region to set the locale information to. Can be a string or an array. It is possible to pass multiple locations.
  • If locales 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 locales is "0", the locale setting is not affected, only the current setting is returned.
  • If locales is followed by additional parameters then each 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 possibly not available locale.
  • If locales is an array then each array element 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 possibly not available locale.

Return Value

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.

Note: The return value of setlocale() depends on the system that PHP is running.

Example: setlocale() example

The example below shows the usage of setlocale() function.

<?php
//setting locale to German
setlocale(LC_ALL, 'de_DE');
echo strftime("The current German time is %r\n");

//setting locale to UK
setlocale(LC_ALL, 'UK');
echo strftime("The current UK time is %r\n");
?>

The output of the above code will be:

The current German time is 03:54:51 PM
The current UK time is 03:54:51 PM

Example: checking locale supported by the system

In the example below, the function is used to check which version of German locale is supported by the system.

<?php
//trying different possible locale names for german
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo "Preferred locale for german on this system is '$loc_de'\n";

//passing locale names as an array
$loc_de = setlocale(LC_ALL, array('de_DE@euro', 'de_DE', 'de', 'ge'));
echo "Preferred locale for german on this system is '$loc_de'\n";
?>

The output of the above code will be:

Preferred locale for german on this system is ''
Preferred locale for german on this system is ''

Example: localized numeric and monetary formatting information

By using localeconv() function, we can get the information about local numeric and monetary formatting. Consider the example below:

<?php
setlocale(LC_ALL, "US");
$locale_info = localeconv();
print_r($locale_info);
?>

The output of the above code will be:

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

)

❮ PHP String Reference