Eva Dias
Eva Dias

Reputation: 1747

change month name to french

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.

Upvotes: 16

Views: 78993

Answers (7)

Eric
Eric

Reputation: 1

Based on the excellent answer from abe1432181 (previously user14321281) I made a small script to make an array with localized month number, short and long name for a year. I use it to fill a dropdown list on a multilingual website.

$locale = 'nl-NL';
$months = [];
$types = ['M', 'MMM', 'MMMM'];
foreach ($types as $tk => $tv) {
    $df = new IntlDateFormatter(
        $locale,
        IntlDateFormatter::NONE,
        IntlDateFormatter::NONE,
        NULL,
        NULL,
        $tv
    );
    for ($i = 1; $i <= 12; $i++) {
        $months[$i][$tk] = str_replace(
            '.',
            '',
            $df->format(mktime(0, 0, 0, $i))
        );
    }
}
print "<pre>";
    print_r($months);
print "</pre>";

Upvotes: 0

Houssem ZITOUN
Houssem ZITOUN

Reputation: 732

strftime function has been deprecated since PHP 8.1.

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

Upvotes: 10

abe1432181
abe1432181

Reputation: 1316

As strftime is now deprecated in php8.1 you need to use an alternative solution - see https://stackoverflow.com/a/57061287/1432181, which uses IntlDateFormatter & setPattern

<?php

$dt='2009-01-01 13:04:01.358';
$ts = new DateTime($dt);

// Example <ISO639-1>_<ISO3166-1> codes...
// English (United Kingdom) en_GB
// English (United States): en_US
// French (France): fr_FR
// German (Germany): de_DE
// Spanish (Spain): es_ES
// Italian (Italy): it_IT
// Chinese (Simplified, China): zh_CN
// Japanese (Japan): ja_JP
// Russian (Russia): ru_RU
// Arabic (Saudi Arabia): ar_SA
// Hindi (India): hi_IN

$formatter = new IntlDateFormatter('fr_FR'
                , IntlDateFormatter::LONG, IntlDateFormatter::LONG);


$cds="G: Era designator (e.g., AD or BC)
y: Year (numeric, 2-digit or more)\n
yy: Year (numeric, 2-digit or more)\n
Y: Week year\n
YY: Week year\n
u: Extended year (as defined by the CLDR)\n
U: Cyclic year name\n
Q: Quarter\n
QQ: Quarter\n
q: Stand-alone quarter\n
qq: Stand-alone quarter\n
M: Month (in text or numeric, short or long form)\n
MM: Month (in text or numeric, short or long form)\n
MMM: Month (in text or numeric, short or long form)\n
MMMM: Month (in text or numeric, short or long form)\n
L: Stand-alone month (in text or numeric, short or long form)\n
LL: Stand-alone month (in text or numeric, short or long form)\n
LLL: Stand-alone month (in text or numeric, short or long form)\n
LLLL: Stand-alone month (in text or numeric, short or long form)\n
w: Week of the year\n
ww: Week of the month\n
W: Week of the month\n
WW: Week of the month\n
d: Day of the month (numeric)\n
dd: Day of the month (numeric)\n
D: Day of the year\n
DD: Day of the year\n
DDD: Day of the year\n
F: Day of the week in month\n
g: Modified Julian day\n
E: Day of the week (text, short or long form)\n
EE: Day of the week (text, short or long form)\n
EEE: Day of the week (text, short or long form)\n
EEEE: Day of the week (text, short or long form)\n
e: Local day of the week (numeric)\n
c: Stand-alone local day of the week (numeric)\n
a: AM/PM marker\n
h: Hour in 1-12 format\n
H: Hour in 0-23 format\n
K: Hour in 0-11 format\n
k: Hour in 1-24 format\n
m: Minute (numeric)\n
mm: Minute (numeric)\n
s: Second (numeric)\n
ss: Second (numeric)\n
S: Fractional second\n
SS: Fractional second\n
SSS: Fractional second\n
A: Milliseconds in day\n
z: Timezone (short form)\n
Z: Timezone (long form)\n
v: Timezone (short specific non-location format)\n
V: Timezone (long generic non-location format)\n
Y: Year with week year\n
";

echo "Test date: $dt";
$parts=explode("\n", $cds);
foreach($parts as $cd) {
    if ( trim($cd)!="" ) {
        $formatter->setPattern(substr($cd,0, strpos($cd, ":")));
        $cd = str_replace("\r", "", $cd);
        $result = $formatter->format($ts);
        echo "<LI><span style='color:grey'>$cd = </span>$result\n";
    }
}

?>

... returns...

Test date: 2009-01-01 13:04:01.358
G: Era designator (e.g., AD or BC) = ap. J.-C.
y: Year (numeric, 2-digit or more) = 2009
yy: Year (numeric, 2-digit or more) = 09
Y: Week year = 2009
YY: Week year = 09
u: Extended year (as defined by the CLDR) = 2009
U: Cyclic year name = 2009
Q: Quarter = 1
QQ: Quarter = 01
q: Stand-alone quarter = 1
qq: Stand-alone quarter = 01
M: Month (in text or numeric, short or long form) = 1
MM: Month (in text or numeric, short or long form) = 01
MMM: Month (in text or numeric, short or long form) = janv.
MMMM: Month (in text or numeric, short or long form) = janvier
L: Stand-alone month (in text or numeric, short or long form) = 1
LL: Stand-alone month (in text or numeric, short or long form) = 01
LLL: Stand-alone month (in text or numeric, short or long form) = janv.
LLLL: Stand-alone month (in text or numeric, short or long form) = janvier
w: Week of the year = 1
ww: Week of the month = 01
W: Week of the month = 1
WW: Week of the month = 01
d: Day of the month (numeric) = 1
dd: Day of the month (numeric) = 01
D: Day of the year = 1
DD: Day of the year = 01
DDD: Day of the year = 001
F: Day of the week in month = 1
g: Modified Julian day = 2454833
E: Day of the week (text, short or long form) = jeu.
EE: Day of the week (text, short or long form) = jeu.
EEE: Day of the week (text, short or long form) = jeu.
EEEE: Day of the week (text, short or long form) = jeudi
e: Local day of the week (numeric) = 4
c: Stand-alone local day of the week (numeric) = 4
a: AM/PM marker = PM
h: Hour in 1-12 format = 1
H: Hour in 0-23 format = 13
K: Hour in 0-11 format = 1
k: Hour in 1-24 format = 13
m: Minute (numeric) = 4
mm: Minute (numeric) = 04
s: Second (numeric) = 1
ss: Second (numeric) = 01
S: Fractional second = 3
SS: Fractional second = 35
SSS: Fractional second = 358
A: Milliseconds in day = 47041358
z: Timezone (short form) = UTC
Z: Timezone (long form) = +0000
v: Timezone (short specific non-location format) = UTC
V: Timezone (long generic non-location format) = UTC
Y: Year with week year = 2009
 PHP Versions and Options (8.2.12)

See https://onlinephp.io/c/f272a for a runtime example

Upvotes: 0

acarito
acarito

Reputation: 640

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

Upvotes: 17

OlivierLarue
OlivierLarue

Reputation: 2410

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

Upvotes: 16

Stefan H Singer
Stefan H Singer

Reputation: 5504

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

Upvotes: 18

Marek Sebera
Marek Sebera

Reputation: 40651

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)

Upvotes: 19

Related Questions