亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在php中顯示月份數組中特定格式的日期

在php中顯示月份數組中特定格式的日期

PHP
HUH函數 2023-09-22 14:46:28
我有一個如下所示的 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",);?>問題陳述:我想要實現的是我想以以下格式顯示日期:08 ao?t 2020對于該月的第一天,將 er 附加到數字后:e.g. 1er ao?t 2020這是我嘗試過的。雖然它可以工作,Line A prints 08 ao?t 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?>
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

PHP 已經幫您解決了:)


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


為了得到這1er一天,你可以做你已經做過的事情。只需拆分 strftime(或其結果)即可。

參考

  • 時間

  • 設置語言環境


另一個解決方案(盡管會按順序顯示所有日期)

$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

參考

  • DateFormatter 的模式

  • 數字格式化程序


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號