nl_langinfo()
Our article is about the PHP function nl_langinfo(), which is used to retrieve locale information. This function is useful for working with different languages
The PHP nl_langinfo() function returns a single piece of locale-specific information — such as the abbreviated name of a weekday, the local date format, or the currency symbol — for the locale that is currently active. It is a thin wrapper around the C library function of the same name, so the exact strings it returns are decided by your operating system's locale database, not by PHP itself.
This page explains the syntax, the most useful item constants, how nl_langinfo() reacts to setlocale(), and the portability caveats you should know before relying on it.
Syntax
string nl_langinfo ( int $item )The function takes one parameter, $item: an integer constant that names the piece of information you want. It returns the corresponding string for the currently selected locale, or false if $item is not valid.
The important idea is that you pass the same constant regardless of language. nl_langinfo(ABDAY_1) always asks for "the abbreviated first weekday name"; whether you get Sun, Dim, or So depends entirely on the locale you set with setlocale(). That is what makes the function useful: your code stays language-agnostic while the output adapts.
Common item constants
The constants are grouped by category. Most systems define these; some are platform-specific.
| Constant | Description |
|---|---|
ABDAY_1 … ABDAY_7 | Abbreviated weekday names, starting at Sunday |
DAY_1 … DAY_7 | Full weekday names |
ABMON_1 … ABMON_12 | Abbreviated month names |
MON_1 … MON_12 | Full month names |
D_T_FMT | Date and time format string (as used by strftime()) |
D_FMT | Date format string |
T_FMT | Time format string |
AM_STR / PM_STR | Strings for AM and PM |
CRNCYSTR | Currency symbol and its position |
YESEXPR / NOEXPR | Regex patterns for an affirmative / negative answer |
Note:
DAY_nandABDAY_nare indexed from Sunday, soABDAY_1is Sunday andABDAY_7is Saturday.
A basic example
Here setlocale() activates the en_US locale, then nl_langinfo(ABDAY_1) reads back the abbreviated name of the first weekday (Sunday) for that locale.
The output is:
SunHow the locale changes the result
Because the return value tracks the active locale, switching locales between calls gives you translated output from the same constant. The constants never change — only setlocale() does.
<?php
// English
setlocale(LC_ALL, 'en_US.UTF-8');
echo nl_langinfo(MON_1), "\n"; // January
// French — same constant, French output
setlocale(LC_ALL, 'fr_FR.UTF-8');
echo nl_langinfo(MON_1), "\n"; // janvierExpected output (when both locales are installed on the system):
January
janvierIf a locale is not installed, setlocale() returns false and the previous locale stays in effect, so you may see the earlier language repeated. Always check the return value of setlocale() in production code.
Reading the date and currency formats
Two of the most practical items are the date/time format string and the currency string. You can feed the format string straight into strftime():
<?php
setlocale(LC_ALL, 'en_US.UTF-8');
$fmt = nl_langinfo(D_T_FMT); // locale's preferred date+time format
echo $fmt, "\n"; // e.g. %a %d %b %Y %r %Z
echo nl_langinfo(CRNCYSTR), "\n"; // e.g. -$ (currency symbol + position flag)The leading character of CRNCYSTR indicates where the symbol goes relative to the number (- = before, + = after, . = in place of the decimal point). For full monetary formatting you usually want localeconv() or money_format() instead, which expose every numeric detail rather than a single string.
Portability and gotchas
- It is not available everywhere.
nl_langinfo()is undefined on Windows and on PHP builds compiled without the Clanginfosupport. Guard withfunction_exists('nl_langinfo')if your code must run cross-platform. - The locale must actually be installed. The constant resolves against the OS locale database; an uninstalled locale silently leaves the previous one active.
setlocale()first. Without an explicitsetlocale(), you get whatever the defaultC/POSIXlocale returns — usually plain English with no formatting niceties.- Constants are integers, not strings. Write
nl_langinfo(ABDAY_1), notnl_langinfo('ABDAY_1').
Related functions
setlocale()— selects the locale thatnl_langinfo()reads from.localeconv()— returns numeric and monetary formatting rules as an array.strftime()— formats a date using a locale format string such as the one fromD_T_FMT.money_format()— formats a number as currency for the active locale.