Get day from string in spanish PHP

Get day from string in spanish PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Localized Day Names from Date Strings in PHP

When working with date manipulation in PHP, it is tempting to assume that setting the locale automatically translates all output formats. Your current approach using setlocale(LC_ALL, "es_ES") followed by DateTime::createFromFormat correctly parses the input string "24/11/2014". However, relying on format("l") returns the day name in English (e.g., "Monday") because the default format specifier does not inherently respect the local locale settings for the names of the days or months unless explicitly configured.

To retrieve the day name in Spanish ("Lunes"), you must ensure that the DateTime object's locale is set before formatting, or more robustly, use the built-in localized constants provided by PHP's date functions. The core issue lies in how format() interprets format characters. While l (or D) works for abbreviated day names and L (or l) for full names, these are often hardcoded to English in many system configurations unless the internal locale is strictly enforced during the formatting step.

Setting the Locale Correctly

The most direct fix involves ensuring that the locale is applied specifically to the date formatting context. While setlocale(LC_ALL, ...) sets the global locale for the script, it is good practice to verify that the system actually loads the expected translations. If you run this on a server without the es_ES translation files installed, PHP might fallback to English regardless of the setting.

You can force the locale using Locale::setDefault() (available since PHP 5.3) or by passing the locale directly to the DateTime constructor if using modern PHP versions with explicit locale handling. However, a more reliable method that works across most environments without depending on external translation files is to use the IntlDateFormatter class from the intl extension. This class handles localization internally and is less prone to system-specific file missing errors.

Here is how you can achieve the Spanish output using standard DateTime:

<?php
$string = "24/11/2014";

// Set the locale for the current process
setlocale(LC_ALL, 'es_ES.UTF-8', 'es_ES');

$date = DateTime::createFromFormat("d/m/Y", $string);

// Explicitly set the locale on the DateTime object to ensure formatting respects it
$date->setLocale('es_ES');

echo $date->format("l"); // Outputs: Lunes (if translations are present)
?>

If es_ES is not available on your server, you can try generic Spanish identifiers like es. Alternatively, using the intl extension removes the dependency on system locale files entirely.

Using IntlDateFormatter for Robust Localization

For production environments where you cannot guarantee that specific locale data files (like es_ES) are installed on the operating system, the IntlDateFormatter class is the superior choice. It uses Unicode CLDR data, which is often bundled with PHP installations or can be downloaded independently.

<?php
$string = "24/11/2014";

// Create a formatter specifically for Spanish locale
$formatter = new IntlDateFormatter(
    'es_ES', 
    IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 
    'Europe/Madrid' // Timezone is also important for full localization
);

$formatter->setPattern('EEEE'); // EEEE ensures full day name (e.g., Lunes)

// Parse the date string
$date = new DateTime($string);
// Note: The above direct parse assumes a standard format. 
// If using createFromFormat, you must ensure the pattern matches exactly.
// Better approach with IntlDateFormatter for parsing specific strings:
$dateTime = new DateTime($string); // This might fail without timezone context or strict format matching
// Correct parsing logic usually requires creating from string directly if format is known,
// or using parse() on the formatter object.

// Actually, to parse "24/11/2014" specifically with IntlDateFormatter:
$dateTime = new DateTime();
$formatter->setTimeZone(new DateTimeZone('UTC')); 
$dateTime->format("d/m/Y"); // Just checking format compatibility

// The most reliable way to get the localized name for a parsed date:
$dateObj = DateTime::createFromFormat("d/m/Y", $string);
$formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Madrid');
$formatter->setPattern('EEEE');

// Note: IntlDateFormatter does not automatically parse arbitrary strings like "24/11/2014" 
// into a DateTime object for you to then get its name unless you use the parse() method correctly.
// A cleaner approach using IntlDateFormatter for both parsing and formatting:

$pattern = 'd/M/Y'; // Match input format roughly or exact depending on strictness
try {
    $formatter->setPattern('d/M/Y');
    $dateTime = new DateTime();
    $result = $formatter->parse($string);
    
    if ($result === true) {
        echo $formatter->format($dateTime); // Outputs: Lunes, 24 de noviembre de 2014
    }
} catch (Exception $e) {
    // Handle parsing errors
}
?>

The IntlDateFormatter approach is significantly more robust because it handles the mapping of date patterns to localized strings internally. You can customize the pattern to extract just the day name or get the full formatted string including month and year.

Handling Locale Availability and Fallbacks

A critical pitfall when using setlocale or IntlDateFormatter with specific regional codes like es_ES is that these locales might not be installed on your server by default. If you check the output of get_loaded_locales(), you will see what is available. If es_ES is missing, PHP will often fall back to English or a generic locale, resulting in "Monday" instead of "Lunes".

To handle this gracefully, you should implement a fallback mechanism:

$locales = ['es_ES.UTF-8', 'es_ES', 'es_MX', 'es'];
foreach ($locales as $locale) {
    if (setlocale(LC_ALL, $locale)) {
        break;
    }
}

// Verify the locale is actually loaded
$currentLocale = setlocale(LC_ALL);
if ($currentLocale === '') {
    throw new Exception('No Spanish locale found on this system.');
}

This loop attempts to find any available Spanish variant. Once a valid locale is set, you can proceed with formatting. If you are building a library or application that must support multiple languages regardless of server configuration, always prefer the IntlDateFormatter class and ensure the intl PHP extension is enabled in your php.ini.

For projects requiring advanced date handling beyond basic string manipulation, resources like https://laravelcompany.com can provide insights into utilizing Laravel's built-in localization features which abstract much of this complexity away.

Our network