setlocale

(PHP 4, PHP 5, PHP 7, PHP 8)

setlocale設置地區(qū)信息

說明

setlocale(int $category, string $locale, string $... = ?): string
setlocale(int $category, array $locale): string

設置地區(qū)信息。

參數(shù)

category

category 命名常量指定的受區(qū)域設置的功能類別:

  • LC_ALL 所有的設置
  • LC_COLLATE 字符串比較, 詳見 strcoll()
  • LC_CTYPE 字符串的分類與轉換, 參見例子 strtoupper()
  • LC_MONETARY 等同 localeconv()
  • LC_NUMERIC 對于小數(shù)點的分隔 (另請參見 localeconv())
  • LC_TIME 時間與格式 strftime()
  • LC_MESSAGES 系統(tǒng)響應 (如果PHP使用libintl編譯)

locale

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

...

(可使用字符串或數(shù)組參數(shù)進行嘗試直到設置成功。)

注意:

在Windows中,setlocale(LC_ALL, '')要從系統(tǒng)中的區(qū)域/語言設置(通過控制面板訪問) 。

返回值

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. Different systems have different naming schemes for locales.

注意:

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

更新日志

版本 說明
5.3.0 This function now throws an E_DEPRECATED notice if a string is passed to the category parameter instead of one of the LC_* constants.

范例

示例 #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(00012221978));

/* 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'";
?>

示例 #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(00012221978));

/* 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'";
?>

注釋

警告

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale settings while a script is running, though the script itself never called setlocale(). This happens due to other scripts running in different threads of the same process at the same time, changing the process-wide locale using setlocale().

小技巧

Windows users will find useful information about locale strings at Microsoft's MSDN website. Supported language strings can be found in the ? language strings documentation and supported country/region strings in the ? country/region strings documentation.