PHP print month in French?

I want to print the date in French, for example:

le 25 février 2014

This is my PHP code that I used, but it does not work.

setlocale(LC_ALL, 'fr_FR');
echo strftime("%A, %e %B, %Y");
echo "<br>";
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %A, %e %B, %Y and");

Output:

January 29, 2014
Wednesday, 29 January, 2014
in French Wednesday, 29 January, 2014 and
+4
source share
6 answers

I usually do a str_replacelike this (for a German, as I don’t know fresh ones).

$date = str_replace(
    array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
   array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'),
   $date
);
-1
source

It works (tested)

What will print:

Current date: Wed Jan 29, 2014
Date in French => Mercredi Jan 29, 2014

You just need to configure it to format it the way you would like.

<?php
// enter date format 2011-01-31 (Y-m-d)
function date_in_french ($date){
$week_name = array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
$month_name=array("","Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août",
"Septembre","Octobre","Novembre","Décembre");

$split = preg_split('/-/', $date);
$year = $split[0];
$month = round($split[1]);
$day = round($split[2]);

$week_day = date("w", mktime(12, 0, 0, $month, $day, $year));
return $date_fr = $week_name[$week_day] .' '. $day .' '. $month_name[$month] .' '. $year;
}
$currentDate=date('Y-m-d');
echo "Current Date: ";
echo date('D')." ".date('d')." ".date('M')." ".date('Y');
echo "<br>";
echo "Date in French => ".date_in_french($currentDate);
+4
source

.. en UTF-8

<? 
setlocale (LC_TIME, 'fr_FR.utf8','fra'); 
echo (strftime("%A %d %B")); 
?>

, .

+1

, , case

function getmounth($_mountnmbr)
{
switch ($_mountnmbr) {
    case '01':
        return 'Janvier';
        break;
    case '02':
        return 'Février';
        break;
    case '03':  
        return 'Mars';
        break;
    case '04':  
        return 'Avril';
        break;
    case '05':
        return 'Mai';
        break;
    case '06':  
        return 'Juin';
        break;
    case '07':  
        return 'Juillet';
        break;
    case '08':
        return 'Août';
        break;
    case '09':  
        return 'Septembre';
        break;
    case '10':  
        return 'Octobre';
        break;
    case '11':
        return 'Novembre';
        break;
    case '12':  
        return 'Décembre';
        break;
  }
}
+1
source

Your system may not have a French translation. I had this problem in the past. For Linux / Ubuntu, here's how to add a locale: https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server

0
source

You can also try an empty string. It should automatically use the language of your OS (of course, it will work only if your OS is in French).

setlocale(LC_TIME, '');
0
source

Source: https://habr.com/ru/post/1524272/


All Articles