简体   繁体   English

如何使用日期在 PHP 中获取星期几?

[英]How to get the name of the day in PHP using date?

Is it possible to get the name of the day in the row of numbers converted to date ?是否可以在转换为date的数字行中获取日期名称?

I have Code:我有代码:

$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;
}

Result:结果:

1 04 2019
2 04 2019
3 04 2019
etc..

What I want is to change it to display the name of the day on that date我想要的是将其更改为显示该日期的日期名称

For Example Date in April:例如四月份的日期:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..

[UPDATE] April 15, 2019 Refer to comments, I see documentation here and apply with mktime(); [更新] 2019 年 4 月 15 日参考评论,我在这里查看文档并使用mktime();

So I Update The Code:所以我更新代码:

$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>';
}

And get the result:并得到结果:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..

You can simplify this a lot without converting back and forth between date and strtotime :您可以大大简化这一点,而无需在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>';
}

See http://php.net/mktime .请参阅http://php.net/mktime

Omitting implicit default values and condensing it a bit, you can in fact boil it down to:省略隐式默认值并将其压缩一点,实际上您可以将其归结为:

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

Note that this code has a minuscule potential to break, should you execute it right at the second in which one month rolls over to the next, and the date('n') and date('t') functions happen to be called "in different months".请注意,如果您在一个月滚动到下一个月的第二秒执行它,并且date('n')date('t')函数恰好被称为“在不同的月份”。 To avoid that possibility entirely, make this operation atomic:为完全避免这种可能性,请将此操作设为原子操作:

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>';
}

The code below will print out what you need..下面的代码将打印出您需要的内容..

$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>';
}

Output:输出:

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

if you change this line如果你改变这一行

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

TO

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

The output will be: Monday, 1st 04 2019输出将是: 2019 年 1 月 1 日星期一

Use:采用:

$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>';;
}

You should try like below.你应该像下面这样尝试。

$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'));
}

After execute you will get result like below.执行后你会得到如下结果。

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

Add the following line to your loop:将以下行添加到循环中:

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

It creates a new DateTime object with your data & echo's the day in the format that you requested.它使用您的数据创建一个新的 DateTime 对象,并以您请求的格式回显日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM