Yankshi
Yankshi

Reputation: 11

Get name of the months in French Language

I have this script :

setLocale(LC_TIME,'fr_FR','fra');

strftime("%d %B %Y);

and this is the result: 10 august 2015.

However, I need the output in French. Any suggestions?

Upvotes: 1

Views: 1007

Answers (3)

Rahul Dhiman
Rahul Dhiman

Reputation: 29

For Meta Tag Tittle:

$month        = array("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre");    

$monthindex      = date("n")-1;

$month         = $month[$monthindex];

$current_date = ucwords(strftime("$month %Y"));

Upvotes: 0

Gras Double
Gras Double

Reputation: 16373

Previously I was using setlocale() and strftime() too, but it has showed itself to be slow and complicated (especially because of the side-effects, you'd want to save and restore previous locale afterwards).

I ended up with a dumb search and replace. You may take this for inspiration:

class MyCarbon extends Carbon\Carbon
{
    public function fr($format)
    {    
        $replacements = [
            'Monday'    => 'lundi',
            'Tuesday'   => 'mardi',
            'Wednesday' => 'mercredi',
            'Thursday'  => 'jeudi',
            'Friday'    => 'vendredi',
            'Saturday'  => 'samedi',
            'Sunday'    => 'dimanche',

            'January'   => 'janvier',
            'February'  => 'février',
            'March'     => 'mars',
            'April'     => 'avril',
            'May'       => 'mai',
            'June'      => 'juin',
            'July'      => 'juillet',
            'August'    => 'août',
            'September' => 'septembre',
            'October'   => 'octobre',
            'November'  => 'novembre',
            'December'  => 'décembre',
        ];

        $from = array_keys($replacements);
        $to = array_values($replacements);

        return str_replace($from, $to, $this->format($format));
    }
}

Upvotes: 0

Emil
Emil

Reputation: 1816

Your code should work. You could do it this way if setlocale is not working on your server and you're not able to fix it:

<?php

    $mos        = array("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre");
    $index      = date("n")-1;
    $mo         = $mos[$index];

    echo date("d ") . $mo . date(" Y");

?>

Result:

10 août 2015

Upvotes: 1

Related Questions