phpdatefrench

How do I get French month names in PHP in this existing script?


I use the following code below to create a select dropdown of the next 12 months, starting with this month:

$year=date("Y");
//Current Month
$month = date('m');
$dateObj   = DateTime::createFromFormat('!m', $month);
$month0 = $dateObj->format('F'); 
$monthHTML .= "<option value='$month/$year'>$month0 $year</option>";
//next 11 months
for ($i=1; $i<=11; $i++) {
    $month = date('m', strtotime('first day of +'.$i.' month'));
        if($month==01) {
            $year++;
        }
    $dateObj   = DateTime::createFromFormat('!m', $month);
    ${"month".$i} = $dateObj->format('F'); 
    $monthHTML .= "<option value='$month/$year'>".${"month".$i}." $year</option>";
}

that creates output like this:

<select id="default-calendar-month" name="default-month">
            <option value='01/2018'>January 2018</option><option value='02/2018'>February 2018</option><option value='03/2018'>March 2018</option><option value='04/2018'>April 2018</option><option value='05/2018'>May 2018</option><option value='06/2018'>June 2018</option><option value='07/2018'>July 2018</option><option value='08/2018'>August 2018</option><option value='09/2018'>September 2018</option><option value='10/2018'>October 2018</option><option value='11/2018'>November 2018</option><option value='12/2018'>December 2018</option></select>

I'd like to use the same code to get the list in French, i.e. like...

<select id="default-calendar-month" name="default-month">
            <option value='01/2018'>Janvier 2018</option><option value='02/2018'>Fevrier 2018</option>

And so on.

Looking at other answers (like this), I tried adding:

setlocale(LC_TIME, "fr_FR");

And changing "strtotime" to "strftime" but that kicks these two errors. What am I doing wrong?

Warning: date() expects parameter 2 to be long, string given in /home/mysite/public_html/add.php on line 31

Fatal error: Call to a member function format() on a non-object in /home/mysite/public_html/add.php on line 36

Solution

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

    this is another approach that i have found somewhere in SO

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