将月份名称更改为法语

15

我有这段代码:

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

但是月份名称以英文显示。我该怎么做才能显示成法语呢?我已经将settings.yml的默认文化设置为法语,但是没有任何变化。

7个回答

19

1
警告:自PhP 8.1起,函数strftime已被弃用。 - Elikill58

18

在php8.1中,strftime已被弃用。 - undefined

16

我知道这已经有点过时了,但对于那些仍在寻找答案的人来说,一个更完整的法语日期回答是:

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

然后

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

或者

echo strftime("%d/%m/%Y", strtotime( $sgps_newsletter->getCreatedAt() ))
//numerical, separated by '/' = 18/9/2013

谢谢指出。已在编辑中修复。 在三个示例中,多了一个括号。不知何故我错过了它,在这个解决方案使用了6年后,没有其他人发现它。 - acarito
在php8.1中,strftime已被弃用。 - undefined

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

1
这是一个正确输出UTF-8的程序。这非常重要,因为法语中有很多带重音符号的字母。谢谢! - eliotRosewater
在php8.1中,strftime已被弃用。 - undefined

7
2022年的回应: strftime 函数自 PHP 8.1 开始已被弃用(可能在 PHP 9 中将被删除)
请尝试使用以下替代方案:
    $ts = new \DateTime();
    $formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM);
    $formatter->setPattern('d MMM Y');

    echo $formatter->format($ts);

1
不要忘记启用扩展以获取它。 - Elikill58

1
使用 strftime 函数来处理时间格式化。

在PHP8.1中,strftime已被弃用。 - undefined

0
由于php8.1中已弃用strftime,您需要使用替代方案-请参考https://dev59.com/JrXna4cB1Zd3GeqPDwiD#57061287,该方案使用IntlDateFormattersetPattern
<?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";
    }
}

?>

...返回...

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)

请参考此链接以查看运行时示例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接