15

I have this code:

<?php 
  echo "'".$sgps_newsletter->getEmail()."' demande en ".date('d.M.Y', strtotime($sgps_newsletter->getCreatedAt())) 
?> 

but the month name is appering in english. What can I do to show it in french? I've change the settings.yml default culture to french, but nothing happens.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Eva Dias
  • 1,709
  • 9
  • 36
  • 67

6 Answers6

20

Use strftime : http://php.net/manual/en/function.strftime.php

<?php
setlocale(LC_TIME, "fr_FR");
echo strftime(" in French %d.%M.%Y and");

(not sure about that %d.%M.%Y but you can read documentation)

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
19

From http://php.net/manual/en/function.date.php:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

OcuS
  • 5,320
  • 3
  • 36
  • 45
Stefan H Singer
  • 5,469
  • 2
  • 25
  • 26
17

I know this is old but for anyone still looking this up, a more complete answer for your date in French would be:

//set locale
setlocale(LC_TIME, "fr_FR"); 

Then

//echo date/formatting based on your locale, in this case, for French
echo strftime("le %d %B, %Y", strtotime( $sgps_newsletter->getCreatedAt() ));
//without day of the week = le 18 septembre, 2013

OR

echo strftime("%A le %d %B, %Y", strtotime( $sgps_newsletter->getCreatedAt() ));
//with day of the week = mercredi le 18 septembre, 2013

OR

echo strftime("%d/%m/%Y", strtotime( $sgps_newsletter->getCreatedAt() ))
//numerical, separated by '/' = 18/9/2013
acarito
  • 634
  • 7
  • 15
  • Thanks for pointing that out. Fixed in edit. In three of the examples there was an extra parenthesis. Somehow I missed it, and in 6 years of this solution being up, no one else caught it. – acarito Mar 19 '19 at 14:54
14
# --------------------
# METHOD 1
# --------------------
# set locale first
setlocale (LC_TIME, 'fr_FR.utf8','fra'); 
// setlocale(LC_TIME, 'fr_FR.UTF8');
// setlocale(LC_TIME, 'fr_FR');
// setlocale(LC_TIME, 'fr');
// setlocale(LC_TIME, 'fra_fra');
# Examples using current time
echo strftime('%Y-%m-%d %H:%M:%S');  // 2015-03-02 17:58:50
echo strftime('%A %d %B %Y, %H:%M'); // lundi 02 mars 2015, 17:58
echo strftime('%d %B %Y');           // 02 mars 2015
echo strftime('%d/%m/%y');           // 02/03/15
# Example with given timestamp
$timestamp = time () ; // Any timestamp will do
echo strftime( "%d %B %Y", $timestamp ) ;  // 02 mars 2015

# --------------------
# METHOD 2 
# --------------------
# using arrays without setting the locale ( not recommanded )
$day = array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"); 
$month = array("janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"); 
// Now
$date = explode('|', date("w|d|n|Y"));
// Given time
$timestamp = time () ;
$date = explode('|', date( "w|d|n|Y", $timestamp ));
echo $day[$date[0]] . ' ' . $date[1] . ' ' . $month[$date[2]-1] . ' ' . $date[3] ; // Lundi 02 mars 2015
OlivierLarue
  • 2,234
  • 27
  • 28
  • 1
    This is the one that correctly outputs as UTF-8. Which is really important as French has a lot of accents on letters. Thank you! – eliotRosewater Nov 25 '22 at 20:45
6

A 2022 response:

strftime function has been deprecated since PHP 8.1 (and maybe will be removed on PHP 9)

Try to use this instead:

    $ts = new \DateTime();
    $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM);
    $formatter->setPattern('d MMM Y');

    echo $formatter->format($ts);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Houssem ZITOUN
  • 644
  • 1
  • 8
  • 23
2

use strftime

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
steve
  • 608
  • 1
  • 5
  • 16