Renish Khunt
Renish Khunt

Reputation: 5824

PHP print Month in French?

I want to print the date in french like:

le 25 février 2014

This is my PHP code I used but it's not working.

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

Upvotes: 4

Views: 7529

Answers (6)

M0ns1f
M0ns1f

Reputation: 2725

There is the function that you are looking for i used 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;
  }
}

Upvotes: 1

Amazone
Amazone

Reputation: 436

First, you must set locale .. en UTF-8

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

If your server don't accept it, you must use array and replace ..

Upvotes: 3

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

This works (tested)

Which will print:

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

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

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

Upvotes: 2

Sebastien C.
Sebastien C.

Reputation: 4833

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

setlocale(LC_TIME, '');

Upvotes: 0

sobyte
sobyte

Reputation: 96

Your system may not have the french translation. I had this problem also 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

Upvotes: 0

Lars Ebert-Harlaar
Lars Ebert-Harlaar

Reputation: 3537

I generally do a str_replace like this (for german, as I do not know fresh).

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

Upvotes: -4

Related Questions