我想更改以下代碼中的日期格式:
#search for all event start dates
$starts = $sxml->xpath('//event/startdate');
#get the unique start dates of these event
$dates = array_unique($starts);
foreach($dates as $date) {
echo "<li class='header'><h1>{$date}</h1></li>" ."\n";
目前,它從 XML 提要中提取為 25/11/2021,我想將其顯示為 2021 年 11 月 25 日星期四 - 可能作為多個變數。但是, date() 不起作用。
任何幫助表示贊賞!
uj5u.com熱心網友回復:
使用 DateTime 物件可以簡單而準確
$dates = ['25/11/2021','24/11/2021','23/11/2021'];
foreach ( $dates as $date){
$df = (DateTime::CreateFromFormat('d/m/Y', $date))->format('l d F Y');
echo "<li class='header'><h1>{$df}</h1></li>" ."\n";
}
結果
<li class='header'><h1>Thursday 25 November 2021</h1></li>
<li class='header'><h1>Wednesday 24 November 2021</h1></li>
<li class='header'><h1>Tuesday 23 November 2021</h1></li>
DateTime 物件的 PHP 手冊
uj5u.com熱心網友回復:
使用日期格式
date_format(DateTimeInterface $object, string $format): string
筆記:
根據檔案:date_format()dd/mm/YY 日期格式不支持
l:周日到周六
j:月中的第幾天(1 到 31)
F:一月到十二月
Y:4 位數字年份
$dates = ['2021/2/11','2021/12/12'];
foreach($dates as $date) {
$d = date_format(date_create($date),'l j F Y');
echo "<li class='header'><h1>${d}</h1></li>" ."\n";
}
輸出
<li class='header'><h1>Thursday 11 February 2021</h1></li>
<li class='header'><h1>Sunday 12 December 2021</h1></li>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365914.html
