Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中以月份数组中的特定格式显示日期_Php_Arrays_Date_French - Fatal编程技术网

在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代码,如下所示,其中有一个法国月份数组

<?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(或其结果)

参考资料

另一个解决方案(将按顺序显示所有日期) 输出 工作

参考资料

我正要发布一些提示,但它们更多的是代码审查。我认为这个问题不适合这个网站,因为它实际上并没有指定一个问题(据您所知,您的代码是有效的);也许可以改为贴在上面?(请务必先阅读他们的帮助页面。)@xNoJustice时区与月份名称有什么关系?你把它和“语言环境”混淆了吗?
<?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