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

时间:2019-04-15 07:55:29

标签: 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..

[UPDATE] 2019年4月15日 请参阅注释,我看到文档here,并与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..

5 个答案:

答案 0 :(得分:1)

您可以简化很多操作,而无需在datestrtotime之间来回转换:

$year = date('Y');
$month = date('n');
$lastDay = date('t');

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}

请参见http://php.net/mktime

省略隐式默认值并将其压缩一下,实际上您可以将其简化为:

foreach (range(1, date('t')) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, date('n'), $day)), '<br>';
}
Mon, 1 04 2019
Tue, 2 04 2019
Wed, 3 04 2019
...
Tue, 30 04 2019

请注意,如果您要在一个月滚动到下一个月的第二秒执行该代码,并且碰巧调用了date('n')date('t')函数,则该代码有可能被破坏“在不同的月份”。为了完全避免这种可能性,请将此操作设为原子操作:

list($year, $month, $lastDay) = explode(' ', date('Y n t'));

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}

答案 1 :(得分:0)

使用:

$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("D-m-Y",strtotime($Year."-".$Month."-".$Date));
  echo $DataDate. '<br>';;
}

答案 2 :(得分:0)

下面的代码将打印出您所需的内容。

$date1 = date('Y-m-01');
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++)
{
    $thatDay = date('Y-m-'.$Date);
    $day = date('l, j', strtotime($thatDay));
    $Month = date('m', strtotime($thatDay));
    $Year = date('Y', strtotime($thatDay));

    echo $day . ' ' . $Month . ' ' . $Year . '<BR>';
}

输出:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
Thursday, 4 04 2019
Friday, 5 04 2019
...
...

如果您更改此行

$day = date('l, j', strtotime($thatDay));

TO

$day = date('l, jS', strtotime($thatDay));

输出为: 2019年1月4日星期一

答案 3 :(得分:0)

您应该尝试以下操作。

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

$StartDate=$date1;
$EndDate=($Year."-".$Month."-".$Maximum_Date);
$EndDate=date('Y-m-d',strtotime($EndDate));

while(strtotime($EndDate)>=strtotime($StartDate))
{
    echo date('l, d-m-Y',strtotime($StartDate))."<br/>";
    $StartDate=date('Y-m-d', strtotime($StartDate . ' +1 day'));
}

执行后,您将得到如下结果。

Monday, 01-04-2019
Tuesday, 02-04-2019
Wednesday, 03-04-2019
...................
...................
...................
...................
Saturday, 27-04-2019
Sunday, 28-04-2019
Monday, 29-04-2019
Tuesday, 30-04-2019

答案 4 :(得分:0)

将以下行添加到循环中:

    echo (new DateTime($Date. '-'. $Month .'-'. $Year))->format('l');

它将创建一个新的DateTime对象,其数据和日期以您所要求的格式显示。