在php中以月份数组中的特定格式显示日期
我有一个php代码,如下所示,其中有一个法国月份数组在php中以月份数组中的特定格式显示日期,php,arrays,date,french,Php,Arrays,Date,French,我有一个php代码,如下所示,其中有一个法国月份数组 <?php $months = array( 1 => "janvier", 2 => "février", 3 => "mars", 4 => "avril", 5 => "mai", 6 => "juin", 7 =>
<?php
$months = array(
1 => "janvier",
2 => "février",
3 => "mars",
4 => "avril",
5 => "mai",
6 => "juin",
7 => "juillet",
8 => "août",
9 => "septembre",
10 => "octobre",
11 => "novembre",
12 => "décembre",
);
?>
对于每月的第一天,将er附加到数字后:
e.g. 1er août 2020
这是我尝试过的。虽然它的工作方式是A行打印08 aot 2020,但我想知道它是否在所有情况下都能工作。这里的所有案例都是指一个月内的所有日子
我已经硬编码了Z行的值,但它会改变
<?php
$months = array(
1 => "janvier",
2 => "février",
3 => "mars",
4 => "avril",
5 => "mai",
6 => "juin",
7 => "juillet",
8 => "août",
9 => "septembre",
10 => "octobre",
11 => "novembre",
12 => "décembre",
);
$this_date="2020-08-08"; // Line Z
$this_time = strtotime($this_date);
$day = date('d', $this_time);
$month = date('n', $this_time);
$month_fr = $months[$month];
$suffix = $day == 1 ? 'er' : '';
$formatted_new_fr = strftime("%d$suffix " . $month_fr . " %Y", $this_time);
echo $formatted_new_fr; // Line A
?>
PHP让您受益匪浅:)
输出
工作
要获得1er
日,您可以做您已经做过的事情。只需拆分strftime(或其结果)
参考资料
另一个解决方案(将按顺序显示所有日期) 输出 工作 参考资料
<?php
$months = array(
1 => "janvier",
2 => "février",
3 => "mars",
4 => "avril",
5 => "mai",
6 => "juin",
7 => "juillet",
8 => "août",
9 => "septembre",
10 => "octobre",
11 => "novembre",
12 => "décembre",
);
$this_date="2020-08-08"; // Line Z
$this_time = strtotime($this_date);
$day = date('d', $this_time);
$month = date('n', $this_time);
$month_fr = $months[$month];
$suffix = $day == 1 ? 'er' : '';
$formatted_new_fr = strftime("%d$suffix " . $month_fr . " %Y", $this_time);
echo $formatted_new_fr; // Line A
?>
setlocale(LC_TIME, 'fr_FR');
$dateString = '2020-08-08';
$timestamp = strtotime($dateString);
$formattedDate = strftime('%d %B %Y', $timestamp);
//setlocale(LC_ALL, Locale::getDefault()); // restore (if neccessary)
echo utf8_encode($formattedDate);
08 août 2020
$locale = 'fr_FR';
$dateString = '2020-08-01';
$date = new DateTimeImmutable($dateString);
$dateFormatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::FULL,
NULL
);
$numberFormatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
$ordinalDay = $numberFormatter->format($date->format('d'));
$dateFormatter->setPattern('LLLL Y');
echo $ordinalDay . ' ' . $dateFormatter->format($date->getTimestamp());
1er août 2020