Home > Net >  Display date in french
Display date in french

Time:02-01

I was wondering if it was posible to display the month in a calender in french ?

     $daysOfWeek = array(' Lundi  ',' Mardi  ','Mercredi',' Jeudi  ','Vendredi',' Samedi ');
 $firstDayOfMonth = mktime(0,0,0,$month,1,$year);
 $numberDays = date('t',$firstDayOfMonth);
 $dateComponents = getdate($firstDayOfMonth);
 $monthName = $dateComponents['month'];

 $dayOfWeek = $dateComponents['wday'];
 if($dayOfWeek==0){
     $dayOfWeek = 6;
 }else{
     $dayOfWeek = $dayOfWeek-1;
 }

 
$datetoday = date('Y-m-d');



$calendar = "<table>";
$calendar .= "<h2>$monthName $year</h2>";

CodePudding user response:

  • If you could use the PECL intl module

    $date = new DateTime();
    $formatter = new IntlDateFormatter('fr_FR');
    $dateFormatter = \IntlDateFormatter::create(
        \Locale::getDefault(),
        \IntlDateFormatter::NONE,
        \IntlDateFormatter::NONE,
        \date_default_timezone_get(),
        \IntlDateFormatter::GREGORIAN,
        'EEEE MMMM'
    );
    var_dump($dateFormatter->format($date)); // string(13) "lundi janvier"
    
  • Else, with a simple array and date('n') and date('N'). See DateTime::format() for formats.

    $days   = ['Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche'];
    $months = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'];
    
    $timestamp = 1643660449; // Example. Should be your mktime()
    $monthName = $months[date('n', $timestamp) - 1];
    $dayName   = $days[date('N', $timestamp) - 1];
    
    echo "$dayName $monthName"; // Lundi Janvier
    
  • Finally, note that strftime() allows to format a date, but is now deprecated.

Some additional notes:

  1. Don't use &nbsp; in your array, but only when displaying your string (maybe take a look to margins/padding using CSS)
  2. The <h2> tag cannot be the first child of a <table> tag.
  •  Tags:  
  • Related