PHP date - get name of the months in local language

PHP date - get name of the months in local language

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Localizing Month Names in PHP Without Hardcoding Strings

The standard date('F') format modifier returns month names in English by default. While this is often acceptable for global applications, it feels disconnected when your target audience speaks a different language, such as Serbian. The immediate temptation is to create a massive associative array mapping numbers 1 through 12 to their Serbian equivalents and manually swap the output whenever a month name is needed.

While that approach works, it violates the principle of DRY (Don't Repeat Yourself) and creates a fragile dependency on your application's logic knowing about calendar data. A more robust solution leverages PHP's built-in internationalization support: the IntlDateFormatter class from the intl extension. This class is part of the core language but requires the underlying ICU library to be installed on the system.

To implement this, you first need to ensure the environment supports it. Run php -m in your terminal; if you do not see intl in the list, you must install the extension for your specific PHP version and OS distribution before proceeding. Once available, the syntax shifts from simple string formatting to object-oriented configuration.

Here is how you can dynamically retrieve a month name in Serbian without hardcoding the data:

$monthNum = 9; // September

// Create a formatter configured for the user's locale or a specific one like 'sr_RS'
$formatter = new IntlDateFormatter(
    null,                    // Use default date style (can be customized)
    IntlDateFormatter::FULL, // Full month name
    IntlDateFormatter::NONE, // No day of week
    'sr_RS',                 // Serbian locale code (supports Latin or Cyrillic script depending on system)
    'default'                // Timezone
);

$monthName = $formatter->format(new DateTime("2023-$monthNum-01"));
echo $monthName; 
// Output will be roughly "септембар" (Cyrillic) or "septembar" (Latin) based on system locale settings.

In this example, we pass the locale sr_RS to the constructor. This tells ICU to return text in Serbian. The first argument, null, allows PHP to inherit the default date style from the application's configuration, but you can explicitly set it to IntlDateFormatter::FULL if you strictly want a full name rather than an abbreviation like "Sep".

A critical detail involves the script encoding. If your server or PHP environment defaults to a Cyrillic-based Serbian locale (like sr_RS@latin vs sr_RS), the output characters will change accordingly. You do not need to worry about this for every request, but you must be aware that the user's browser or OS locale settings can influence which variant of Serbian is used if you pass null as the first argument in some configurations. Explicitly setting 'sr_RS' ensures consistency regardless of the server's default locale.

Another common pitfall arises when using date('F') in a legacy codebase where the intl extension is not loaded. If you attempt to instantiate IntlDateFormatter without checking for the extension, your application will crash with a fatal error. In production environments, always wrap this logic in a check or ensure the dependency is met during deployment.

For developers using the Laravel framework, there is a built-in localization system that abstracts this away entirely. The __('month_name', ['date' => $date]) helper combined with Carbon instances allows you to access localized names directly from your resources/lang files. This means you can define Serbian translations in a single file and have Laravel automatically resolve them, keeping your logic clean and decoupled from low-level date formatting rules.

Ultimately, relying on the intl extension or framework helpers is superior to manual string substitution because it respects regional variations (like Cyrillic vs. Latin script) that a hardcoded array would miss. This approach ensures your application remains culturally relevant without bloating your codebase with static data.

Our network