Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ;)

like image 621
JeroenM Avatar asked Oct 19 '25 22:10

JeroenM


2 Answers

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";
}
?>
like image 134
Yasen Zhelev Avatar answered Oct 22 '25 13:10

Yasen Zhelev


I used str_replace instead now. It was (for me) an easy and fast way to get the output i needed. But I've only been programming for nearly a year now, so probably my way of fixing it was not the best one. But it works for now (: Thanks to eveyone who Replied to the thread!

This is what i got now using str_replace.

<?php
$d = strtotime('today');
setlocale (LC_TIME,'NL_nl');
ini_set('intl.default_locale', 'nl_NL');
switch ($d) {
    case strtotime('monday'):
    case strtotime('friday'):
    case strtotime('saturday'):
    case strtotime('sunday'):
        $ff = strtotime('next friday');
        $ffn = date('l d F ', $ff);
        $dutch = str_replace (
            array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
            array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
            $ffn);
        echo $dutch;
    break;
    case strtotime('tuesday'):
    case strtotime('wednesday'):
    case strtotime('thursday'):
        $ff = strtotime('next friday' . '+ 7days');
        $fft = date('l d F', $ff);
        $dutch = str_replace (
            array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
            array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
            $fft);
        echo $dutch;
    break;
    default:
        echo "Something went wrong";
}
?>
like image 27
JeroenM Avatar answered Oct 22 '25 14:10

JeroenM