Open In App

PHP | setlocale() Function

Last Updated : 31 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

php




<?php
 
// Setting locale to german
setlocale(LC_ALL,"de");
echo strftime("The current german time is %r");
 
// Setting locale to english
setlocale(LC_ALL,"en");
echo strftime(" and the current english time is %r");
?>


Output: 
 

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. 
 

php




<?php
 
// Try different possible locale names for german
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
echo "Preferred locale for german on this system is '$loc_de'";
?>


Output: 
 

Preferred locale for german on this system is 'German_Germany.1252'

Example 3: Simple program to use LC_MONETARY 
 

php




<?php
 
// Setting locale to english
setlocale(LC_MONETARY,"en");
$loc=localeconv();
print_r($loc);
?>


Output: 
 

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

)

Reference: https://www.php.net/manual/en/function.setlocale.php 
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads