PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<rtrimsha1_file>
view the version of this page
Last updated: Sun, 02 May 2004

setlocale

(PHP 3, PHP 4 , PHP 5)

setlocale -- Set locale information

Description

string setlocale ( mixed category, string locale [, string ...])

string setlocale ( mixed category, array locale)

category is a named constant (or string) 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()

  • LC_CTYPE for character classification and conversion, for example strtoupper()

  • LC_MONETARY for localeconv()

  • LC_NUMERIC for decimal separator (See also localeconv())

  • LC_TIME for date and time formatting with strftime()

Nota: As of PHP 4.2.0, passing category as a string is deprecated, use the above constants instead. Passing them as a string (within quotes) will result in a warning message.

If locale is 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 NULL or "0", the locale setting is not affected, only the current setting is returned.

If locale is an array or followed by additional parameters then each array element or 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.

Nota: Passing multiple locales is not available before PHP 4.3.0

Setlocale 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. An invalid category name also causes a warning message. Category/locale names can be found in RFC 1766 and ISO 639.

Nota: The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.

Suggerimento: Windows users will find useful information about locale strings at Microsoft's MSDNwebsite. Supported language strings can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_language_strings.asp and supported country/region strings at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp. Windows systems support the three letter codes for country/region specified by ISO 3166-Alpha-3, which can be found at this Unicode website .

Esempio 1. setlocale() Examples

<?php
/* Set locale to Dutch */
setlocale(LC_ALL, 'nl_NL');

/* Output: vrijdag 22 december 1978 */
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));

/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo
"Preferred locale for german on this system is '$loc_de'";
?>

Esempio 2. setlocale() Examples for Windows

<?php
/* Set locale to Dutch */
setlocale(LC_ALL, 'nld_nld');

/* Output: vrijdag 22 december 1978 */
echo strftime("%A %d %B %Y", mktime(0, 0, 0, 12, 22, 1978));

/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
echo
"Preferred locale for german on this system is '$loc_de'";
?>



add a note add a note User Contributed Notes
setlocale
Melvin D. Nava
12-Apr-2004 10:13
Get the Current Date in Spanish Format

<?php
setlocale
(LC_TIME,"spanish");
$long_date = str_replace("De","de",ucwords(strftime("%A, %d de %B de %Y")));
echo
$long_date;
?>

Will print: Lunes, 12 de Abril de 2004
As seen in: http://mdnava.network.com.ve/

Bare in mind that this still depends on the server local time, editor should delete my other post. Or this one if he prefers.
MagicalTux at FF.st
06-Apr-2004 03:37
Be careful when using LC_ALL or LC_NUMERIC ! For example if you want to pass a numeric value (example : 18.4) in a GET/POST form, you'll probably use :

<input type="text" name="numeric" value="<?=floatval($num);?>" />

If you use locale fr_FR at this time, $_GET['numeric'] will have value string(18,4) ... if you convert it back to float without re-doing a setlocale, PHP will truncate the value and you'll get only 18 ...
And the main problem can be if you do cross-sites data transfert. Example : I have a website that uses locale from the user settings. If the user have a French locale, I'll get something like 18,4 . If I pass that to paypal (pratical example) it will not work. So the best is to always set LC_NUMERIC to "C" (posix).

Little example code :
<?php
$val
=18.4; // any float value
echo 'Initial: '.$val."\n";

setlocale(LC_NUMERIC,'fr_FR');
$val=strval($val); // convert $val to string
echo 'French: '.$val."\n";

setlocale(LC_NUMERIC,'C'); // posix - note that $val is still a string !!!
$val=floatval($val); // convert it back to float
echo 'Bad convertion: '.$val."\n"; // !
?>

Output :

Initial: 18.4
French: 18,4
Bad convertion: 18
zyxwvu at users dot sourceforge dot net
02-Apr-2004 07:55
If you're a Windows PHP programmer and you want to use ISO encoding, not default windows-125x, you have to use one of these codepages:

28591 gives iso-8859-1
28592 gives iso-8859-2
28593 gives iso-8859-3

etc.

28605 gives iso-8895-15

Example:

// Set to polish ISO-8859-2
setlocale(LC_ALL, 'Polish_Poland.28592');

Of course you have to activate required codepages in the Start -> Control Panel -> Internationalization -> Advanced, and be sure that your editor generates documents in the iso encoding :).
bogdan at iHost dot ro
02-Mar-2004 08:53
On some systems (at least FreeBSD 4.x) the format for a `locale' is, for example, ro_RO.ISO8859-2. If you use ro_RO instead setlocale will return FALSE. Just browse in /usr/share/locale and see what is the name of the directory holding your `locale' and use that name in your scripts:

<?php
  clearstatcache
();
 
$pos = strrpos ($_SERVER["PHP_SELF"], "/");
 
$fisier = substr ($_SERVER["PHP_SELF"], $pos + 1);
 
$result = filemtime ($fisier);
 
$local = setlocale (LC_TIME, 'ro_RO.ISO8859-2');
  if (
$local == "ro_RO.ISO8859-2") {
  
$modtime = strftime '%e&nbsp;%B&nbsp;%Y&nbsp;%H:%M', $result);
  } else {
  
$modtime = strftime ('%d.%m.%Y&nbsp;%H:%M', $result);
  }
 
printf ("Ultima&nbsp;actualizare: %s\\n", $modtime);
?>
Melvin D. Nava
20-Feb-2004 11:24
Get the Current Date in Spanish Format

setlocale (LC_TIME,"spanish");
$long_date = str_replace("De","de",ucwords(strftime("%A, %d de %B de %Y")));

Is that easy!!
Magnus Hammar
18-Feb-2004 12:12
For MacOS X 10.2 users, try this to enable setlocale:
http://www.macmax.org/locales/index_en.html
mk at totu dot com
26-Jan-2004 10:59
Be carefull - setting a locale which uses commas instead of dots in numbers may cause a mysql db not to understand the query:
<?php
setlocale
(LC_ALL,"pl");
$price = 1234 / 100; // now the price looks like 12,34
$query = mysql_query("SELECT Id FROM table WHERE price='".$price."'");
?>
Even if there is a price 12.34 - nothing will be found
mbirth at webwriters dot de
24-Jan-2004 02:00
I noticed that PHP sometimes doesn't switch the locale as it should. Although I used <?php setlocale(LC_TIME, 'German'); ?>, I sometimes got e.g. "Monday" instead of "Montag" for the day of week. So I now set the locale using the following do ... while:

<?php

$locale
= 'de_DE.ISO8859-1';
do {
 
$locale_result = setlocale(LC_TIME, $locale);
} while (
$locale_result != $locale);

?>
kommissar at zonapowers dot cl
13-Jan-2004 07:37
Just for taking property about the language; write the name of your language where is es_CL (Chile). But I am now fighting with the PHP script trying to get the chilean time, and not the server time.

<?php
setlocale
(LC_TIME,"es_CL");
$date = strftime("%A %d de %B de %Y");
echo
$date;
?>
Adam Rice
02-Dec-2003 03:13
OS X 10.2 does not have complete locale support: navigating to /usr/share/locale shows all the locale directories one might expect, but these don't contain all the same files as other flavors of Unix.

I haven't figured out where to get the necessary resources, or how to install them, but if I do, I'll post that here.

Not sure if this has been fixed in 10.3.
thomas at hannibalsoftware dot dk
22-Nov-2003 11:34
In hope to help other danes (who else would need our lang.code?!), here is the language-code for FreeBSD:

setlocale (LC_ALL, 'da_DK.ISO8859-1');

I got the info from a user of RedHat so I guess that it works with that too!

/Hannibal
Duke Ionescu <duke at mastre dot com>
12-Aug-2003 08:16
BTW, the standard US locale is:

   en_US.iso885915

Just in case you need to set it on a system that has it set to POSIX (in which case PHP returns "C") and either you don't or can't change it system-wide.
stefan DO NOT SPAM @ netautor NOPE com
09-Feb-2003 10:33
On a Win98 / Apache 2 / PHP 4.3.0 / Zend Optimizer system works the following : setlocale(LC_ALL,"ge") instead of de_DE as on any other system. why i'm not surprised ;-)
bartolli at plovdiv-news dot com
10-Jan-2003 11:06
For Bulgaria Time Zone:
<?
setlocale
(LC_TIME, 'bg_BG');
echo
strftime ("%A, %e %B %Y")
?>

output = "Ден Дата Месец Година"
example --> Петък, 10 Януари 2003
ekekec79 at yahoo dot com
02-Jan-2003 05:45
For Turkey time zone, the
setlocale(LC_TIME, 'turkish');
works well.
info at zeltru-design dot de
29-Dec-2002 07:42
On Windows XP (maybe W2K too) set it to "German", and on an Unixserver it work with "de".

Primary I was confused, cause everyone say it should be "de_DE".

No matter, the settings above works perfektly.
r dot nospam dot velseboer at quicknet dot nospam dot nl
09-Sep-2002 01:02
be careful with the LC_ALL setting, as it may introduce some unwanted conversions. For example, I used

setlocale (LC_ALL, "Dutch");

to get my weekdays in dutch on the page. From that moment on (as I found out many hours later) my floating point values from MYSQL where interpreted as integers because the Dutch locale wants a comma (,) instead of a point (.) before the decimals. I tried printf, number_format, floatval.... all to no avail. 1.50 was always printed as 1.00 :(

When I set my locale to :

 setlocale (LC_TIME, "Dutch");

my weekdays are good now and my floating point values too.

I hope I can save some people the trouble of figuring this out by themselves.

Rob
Gabor Deri <gaborDOTderi at fuggetlen dot hu>
07-Aug-2002 03:31
IMPORTANT notice for DEBIAN linux users:

after the upgrade to the new stable (woody), you may have encountered that setlocale is not working at all, even though you have the files in the /usr/share/locale directory and everything was fine before the upgrade. in this case look at the /etc/locale.gen file, which contains the generated locales. if it is empty, you do not have any useful locales. to generate the needed locales run 'dpkg-reconfigure locales' as root and select the locales you need.
nospam at please dot com
19-Jul-2002 02:22
My server runs freebsd 4.6-stable, apache2 + php4.2.1 module

The only way to get locale settings working was to define the 'package' directly:
setlocale (LC_ALL, 'nl_NL.ISO8859-1');

I saw that no alias file existed in /usr/share/locale maybe it could be defined

I hope this helps some people :)
jorg-spamm at omnimedia dot no
03-Jul-2002 05:23
I needed to compile and install some extra locales to get this to work on RH7.3. Probably just me not doing a proper installation, but this is what it took to fix it:

localedef -ci no_NO -f ISO_8859-1 no_NO
elindset at hoved dot net
12-May-2002 08:59
In FreeBSD I had to use no_NO.ISO8859-1 instead of just no_NO..

<?PHP
   setlocale
(LC_ALL, 'no_NO.ISO8859-1');
   echo
strftime ("%A %e %B %Y", time());
?>
27-Jul-2001 09:34
Warning ! As specified in the man page, "The locale state is common to all threads within a process". This may be a limitation for PHP, because setlocale() stores this state within global variables shared by all threads instead of allocating a thread-level safe variable. If PHP runs in SAPI mode with enabled support for mulyithreading, this will break some PHP paradygmes... On some platforms only will the locale be set in thread-safe mode. Test your PHP installation in SAPI mode (integrated with a multithreaded web-server) by running concurrent scripts on the same web server, each of them setting a different locale and sleeping for a while before retreiving the current locale. This limitation does not applied to PHP running with a CGI interface (i.e. autonomous process).
misc dot anders at feder dot dk
19-Jun-2001 12:13
Under FreeBSD, locale definitions are stored in the /usr/share/locale/ directory. Danish time formats and weekdays, for instance, are stored in /usr/share/locale/da_DK.ISO_8859-1/LC_TIME.
Fabian dot Rodriguez at toxik dot com
15-Dec-2000 01:20
BTW, not all locales are always supported on every server. Ask you ISP/network admin to see if they are.
Fabian dot Rodriguez at toxik dot com
15-Dec-2000 01:19
Language information is defined by this RFC:
RFC1766 - Tags for the Identification of Languages
 - http://www.ietf.org/rfc/rfc1766.txt

And the 2 letter codes come from:
ISO639: "Code for the representation of names of languages"
 - http://www.w3.org/WAI/ER/IG/ert/iso639.htm
ISO3166-1: "English country names and code elements"
 - http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/index.html

Also note that the language name can be used in full (ex: setlocale('LC_TIME','swedish'); ) . This full name comes from the latest mentioned file.
noog at libero dot it
23-Nov-2000 09:13
On windows:
Control Panel->International Settings
You can set your locale and customize it
And locale-related PHP functions work perfectly
Morgan Christiansson &lt;mog at linux dot nu&gt;
29-Mar-2000 07:56
check /usr/share/locale/ if you want more info about the locale available with your *NIX box

there is also a file called /usr/share/locale/locale.alias with a list of aliases
such as swedish for sv_SE

so on all boxes i have accounts on (rh 6.0 and slack 3.4) you can just use setlocale("LC_ALL","swedish"); or other prefered language in plain english.

However, the weekdays were in all lowercase :(

Note: export LC_ALL=swedish made a lot of programs swedish for me, it's also possible to make them russian or japanese :)
Lucas Thompson <lucas at slf dot cx>
31-Jan-2000 08:57
The Open Group has an excellent document available on the setlocale() library function, most of which applies to the PHP function of the same name.

http://www.opengroup.org/onlinepubs/7908799/xbd/locale.html

WARNING: This document might be a little too complex for people who came from HTML to PHP.

If you migrated from the world of C programming you'll be a locale master after reading this document.
Michal Fita <manveru at witrynka dot pl>
11-Jan-1999 05:41
It is written nowhere, that setlocale() can be used for simple mathematical  operations like adding, substracting, dividing or multiplying. The code below gives correct results:

setlocale(LC_ALL, "pl");
$net = "1234,56";
$gross = "1,22" * $net;
printf("gross: %s, net: %s", $gross, $net");
/* Polish VAT tax is 22% */

Result of this code is:
gross: 1234,56, net: 1506,1632
So, every one can use his own decimal point for mathematical operations without any error or warning (PHP3.0.3).

<rtrimsha1_file>
 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST