Open In App

PHP | setlocale() Function

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:
 



Below examples illustrate the setlocale() function in PHP:
Example 1: A simple program to generate the locale defined time. 
 




<?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
 
// 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
 
// 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 
 


Article Tags :