Home » Php » php – setlocale and strftime not translating month

php – setlocale and strftime not translating month

Posted by: admin July 12, 2020 Leave a comment

Questions:

My code:

setlocale(LC_TIME, 'ca_ES');
echo strftime("%#d %B", strtotime($ticket->date_created));

outputs something like:

28 August

instead of my expectation:

28 Agost

I expect “Agost” because that is Catalan language (set through setlocale()).

Is this how setlocale and strftime is supposed to work?

FYI: My local development machine is Windows 7, set to locale: en-PH

How to&Answers:

The locale names are different on Windows. It looks like it’s defaulting to English.

Try the full length name (you can try several and it will pick the first found):

setlocale(LC_TIME, 'ca_ES', 'Catalan_Spain', 'Catalan');

You may look at this table, which may be helpful : http://docs.moodle.org/dev/Table_of_locales

Answer:

On Ubuntu 14.04 setlocale(LC_TIME, 'de_DE.UTF8'); did the trick for me to get a german month format/name 🙂

Answer:

strftime gives localized versions of date formats. If you do get unexpected results, most probably the localized version you are expecting is not on your system.

I have no experience with windows, but on my Debian Linux server, I had to install the localized strings I wanted.

Answer:

On Debian 9 setlocale(LC_TIME, 'fr_FR.UTF8'); did the job for me to get nicely french formatted day/month display. Make sure to have the locales installed with the following sequence:

apt-get install locales

dpkg-reconfigure locales

Answer:

You need to install the locale you want in your server.

  1. SSH into your server
  2. Check which locales are installed: locale -a
  3. if your locale is not there, install it: sudo locale-gen ca_ES
  4. Update the locales in your server: sudo update-locale
  5. Restart apache for the changes to take effect: sudo service apache2 restart

That is it!

Answer:

For all those, like me, who still cannot make strftime function due to server restrictions… Not perfect but it works.

$months[1] = "gennaio";
$months[2] = "febbraio";
$months[3] = "marzo";
$months[4] = "aprile";
$months[5] = "maggio";
$months[6] = "giugno";
$months[7] = "luglio";
$months[8] = "agosto";
$months[9] = "settembre";
$months[10] = "ottobre";
$months[11] = "novembre";
$months[12] = "dicembre";

// giorni
$weekdays[0] = "domenica";
$weekdays[1] = "lunedì";
$weekdays[2] = "martedì";
$weekdays[3] = "mercoledì";
$weekdays[4] = "giovedì";
$weekdays[5] = "venerdì";
$weekdays[6] = "sabato";

function strftimeIta($format, $timestamp){

    global $weekdays, $months;

    preg_match_all('/%([a-zA-Z])/', $format, $results);

    $originals = $results[0];
    $factors = $results[1];


    foreach($factors as $key=>$factor){
        switch($factor){
            case 'a':
                /*** Abbreviated textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'A':
                /*** Full textual representation of the day ***/
                $n = date('w', $timestamp); // number of the weekday (0 for sunday, 6 for saturday);
                $replace = ucfirst($weekdays[$n]);
                break;
            case 'h':
            case 'b':
                /*** Abbreviated month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                $replace = substr($replace, 0, 3);
                break;
            case 'B':
                /*** Full month name ***/
                $n = date('n', $timestamp); // Numeric representation of a month, without leading zeros
                $replace = ucfirst($months[$n]);
                break;
            default:
                /*** Use standard strftime function ***/
                $replace = strftime("%".$factor, $timestamp);
                break;
        }
        $search = $originals[$key];
        $format = str_replace($search, $replace, $format);
    }
    return $format;
}

Answer:

I know that this is an old question, but I thought I’d contribute anyway.

If you can SSH into the server that you’re working on, then write: locale -a and then you can see your available locales. If you’re language isn’t listed, then it need to be installed.

For me, I saw the one I was looking for: nb_NO.utf8 in the list, so I wrote it exactly how it was written: setlocale( LC_ALL, 'nb_NO.utf8' ) and that worked for me.