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:
|
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.
|
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.
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