Search code examples
phpdatestrftimesetlocale

Name of day/month that's not today in another language in php


Im trying to get the name of a day in php in another language than English. I changed the locale settings to setlocale(LC_ALL, 'nl_NL'); (this returns true) but the date still shows in English. I then found out you should use strftime() for it to work, but that just returns the current time in the set language, and I'm trying to loop trough a number of predefined days, so the output would be

Vrijdag 30 augustus

Zaterdag 31 augustus

Zondag 1 september

etc.

Subtracting/adding days from the current timestamp is also not an option for what I want to do.


Solution

  • Whenever you need to manipulate date/time stamps based on locale, you should use strftime

    also change the encoding to utf-8

    http://php.net/manual/en/function.strftime.php

    example :

    <?php
    header('Content-Type: text/html; charset=UTF-8');
    
    $myDate = "Feb 21, 2013";
    
    $locale = 'fr_FR.utf8';
    setlocale(LC_ALL, $locale);
    echo strftime('%d %B %Y', strtotime($myDate));  
    
    $locale = 'en_US.utf8';
    setlocale(LC_ALL, $locale);
    echo strftime('%d %B %Y', strtotime($myDate));
    ?>