如何使用日期在PHP中获取日期名称?

是否可以将数字行中的日期转换为date?


我有代码:


$date1 = date('Y-m-01');

$Month = date('m', strtotime($date1));

$Year = date('Y', strtotime($date1));

$Maximum_Date = date('t', strtotime($date1));


for($Date = 1; $Date <= $Maximum_Date; $Date++){

  $DataDate = $Date . ' ' . $Month . ' ' . $Year . '<BR>';

  echo $DataDate;

}

结果 :


1 04 2019

2 04 2019

3 04 2019

etc..

我想要将其更改为显示该日期的日期


例如4月的日期:


Monday, 1 04 2019

Tuesday, 2 04 2019

Wednesday, 3 04 2019

etc..

[更新] 2019年4月15日, 请参阅评论,我在这里查看文档并通过mktime();


所以我更新代码:


$date1 = date('Y-m-01');

$Month = date('m', strtotime($date1));

$Year = date('Y', strtotime($date1));

$Maximum_Date = date('t', strtotime($date1));


for($Date = 1; $Date <= $Maximum_Date; $Date++){

  echo date("l, d m Y", mktime(0, 0, 0, $Month, $Date, $Year)) . '<br>';

}

并得到结果:


Monday, 1 04 2019

Tuesday, 2 04 2019

Wednesday, 3 04 2019

etc..


慕虎7371278
浏览 125回答 3
3回答

米脂

将以下行添加到循环中:&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;(new&nbsp;DateTime($Date.&nbsp;'-'.&nbsp;$Month&nbsp;.'-'.&nbsp;$Year))->format('l');它将使用您的数据和日期作为您请求的格式创建一个新的DateTime对象。

慕田峪4524236

下面的代码将打印出您所需要的。$date1 = date('Y-m-01');$Maximum_Date = date('t', strtotime($date1));for($Date = 1; $Date <= $Maximum_Date; $Date++){&nbsp; &nbsp; $thatDay = date('Y-m-'.$Date);&nbsp; &nbsp; $day = date('l, j', strtotime($thatDay));&nbsp; &nbsp; $Month = date('m', strtotime($thatDay));&nbsp; &nbsp; $Year = date('Y', strtotime($thatDay));&nbsp; &nbsp; echo $day . ' ' . $Month . ' ' . $Year . '<BR>';}输出:Monday, 1 04 2019Tuesday, 2 04 2019Wednesday, 3 04 2019Thursday, 4 04 2019Friday, 5 04 2019......如果您更改此行$day = date('l, j', strtotime($thatDay));至$day = date('l, jS', strtotime($thatDay));输出将是:2019年1月1日,星期一
打开App,查看更多内容
随时随地看视频慕课网APP