Search code examples
phpdatestrtotimestrftimesetlocale

Php: Change language in date/strftime/strtotime


I'm trying to get the output on my second piece of code to be Dutch. The first code gives the right output, but in English. I tried to get it in Dutch but usually i ended up with 01-01-1970, 12-12-2015 or nothing. The code should give next friday this week, unless its past monday, then it's fridat next week.

My working code but in English.

<?php
$d = strtotime('today');

switch ($d) {
    case strtotime('monday'):
    case strtotime('friday'):
    case strtotime('saturday'):
    case strtotime('sunday'):
        $ff = strtotime('next friday');
        $ffn = date('l d M', $ff);
        echo $ffn;
    break;
    case strtotime('tuesday'):
    case strtotime('wednesday'):
    case strtotime('thursday'):
        $ff = strtotime('next friday' . '+ 7days');
        $fft = date('l d M', $ff);
        echo $fft;
    break;
    default:
        echo "Something went wrong";
}
?>

My code which gives a Dutch output, but the wrong date (it gives today's date, not next friday/friday next week.)

By the way, this is my first question ive asked here, so I might be a bit vague or something ;)


Solution

  • Just output the date using strftime() and set_locale() functions. No need to change your code logic if it is correct.

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

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

    <?php
    $d = strtotime('today');
    
    $format = '%A %d %b';
    setlocale(LC_TIME, 'NL_nl');    
    setlocale(LC_ALL, 'nl_NL');
    
    switch ($d) {
        case strtotime('monday'):
        case strtotime('friday'):
        case strtotime('saturday'):
        case strtotime('sunday'):
            $ff = strtotime('next friday');
            $ffn = strftime($format, $ff);
            echo $ffn;
        break;
        case strtotime('tuesday'):
        case strtotime('wednesday'):
        case strtotime('thursday'):
            $ff = strtotime('next friday' . '+ 7days');
            $fft = strftime($format, $ff);
            echo $fft;
        break;
        default:
            echo "Something went wrong";
    }
    ?>