phpdatesetlocale

PHP setlocale has no effect


The setlocale() function doesn't set the desired language (German).


The goal is to output month names.

This is my test code with trials so far:

<?php

date_default_timezone_set('Europe/Berlin');
setlocale(LC_ALL, 'de_DE.utf8');
// Or
setlocale(LC_ALL, 'de_DE@euro');
// Or
setlocale(LC_ALL, 'de_DE');
// Or
setlocale(LC_ALL, 'de');
// Or
setlocale(LC_ALL, 'ge');


echo strftime('%B');

Output:

June

instead of

Juni

Any suggestions?

PHP version 5.6


Solution

  • This solution might help if you don't have shell access to the server.

    If you have shell access, then Benjamin Seiller's answer is the best!

    As I don't have any other possibilities (shell), I've found a solution with only PHP by using the IntlDateFormatter class.

    <?php
    
    // Example vars
    $month = '6';
    $year = '2014';
    
    $fmt = new IntlDateFormatter('de_DE',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'Europe/Berlin',
        IntlDateFormatter::GREGORIAN);
    
    $lastMonth = mktime(0, 0, 0, $month -1, 1, $year);
    
    $showLastMonth =  $fmt->format($lastMonth);
    echo $showLastMonth;