PHP print Month in French?

前端 未结 6 829
独厮守ぢ
独厮守ぢ 2020-12-18 10:27

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_         


        
相关标签:
6条回答
  • 2020-12-18 11:04

    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 ..

    0 讨论(0)
  • 2020-12-18 11:09

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-18 11:12

    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
    );
    
    0 讨论(0)
  • 2020-12-18 11:14

    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);
    
    0 讨论(0)
  • 2020-12-18 11:19

    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

    0 讨论(0)
  • 2020-12-18 11:29

    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, '');
    
    0 讨论(0)
提交回复
热议问题