W3docs

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.

ConstantDescription
ABDAY_1ABDAY_7Abbreviated weekday names, starting at Sunday
DAY_1DAY_7Full weekday names
ABMON_1ABMON_12Abbreviated month names
MON_1MON_12Full month names
D_T_FMTDate and time format string (as used by strftime())
D_FMTDate format string
T_FMTTime format string
AM_STR / PM_STRStrings for AM and PM
CRNCYSTRCurrency symbol and its position
YESEXPR / NOEXPRRegex patterns for an affirmative / negative answer

Note: DAY_n and ABDAY_n are indexed from Sunday, so ABDAY_1 is Sunday and ABDAY_7 is Saturday.

A basic example

php— editable, runs on the server

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:

Sun

How 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";   // janvier

Expected output (when both locales are installed on the system):

January
janvier

If 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 C langinfo support. Guard with function_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 explicit setlocale(), you get whatever the default C/POSIX locale returns — usually plain English with no formatting niceties.
  • Constants are integers, not strings. Write nl_langinfo(ABDAY_1), not nl_langinfo('ABDAY_1').
  • setlocale() — selects the locale that nl_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 from D_T_FMT.
  • money_format() — formats a number as currency for the active locale.

Practice

Practice
What does the nl_langinfo() function do in PHP?
What does the nl_langinfo() function do in PHP?
Was this page helpful?