获取最近7天的一周中的哪一天?

时间:2022-10-21 10:29:38

I need the names of the day (Monday, Tuesday, Wednesday, Thuesday, Friday, Saturday, Today).

我需要当天的名字(星期一,星期二,星期三,星期二,星期五,星期六,今天)。

I know this is a newby question and PHP has a date() function. But I tried and can't figure out how...

我知道这是一个新问题,PHP有一个date()函数。但我尝试了,无法弄清楚如何......

2 个解决方案

#1


0  

According to the PHP Manual at http://php.net/manual/en/function.date.php, just use "l" as the format parameter to get the full name of the day.

根据http://php.net/manual/en/function.date.php上的PHP手册,只需使用“l”作为格式参数即可获取当天的全名。

So 23rd Mar 2014 is a Sunday, as echoed by

因此,2014年3月23日是一个星期天

<?php
echo date ("l", mktime(0, 0, 0, 3, 23, 2014));
// Echoes Sunday
?>

To get past 7, 6, 5 or 10000 days (or number of days in the future) from the current day, according the information at this page, just use negative or positive integers in a string in the strtotime function:

要从当天开始过去7天,6天,5天或10000天(或将来的天数),根据此页面上的信息,只需在strtotime函数中的字符串中使用负整数或正整数:

<?php
$backcount = -4;
echo date ("l", strtotime("$backcount day"));
// Executed on 23 Mar 2014 will give Wednesday
?>

Knowing this, you can apply a for loop to get what you need. And if want "Today" instead of the full name of the current day, just add an if condition to handle the situation where the backcount variable is zero.

知道这一点,你可以应用for循环来获得你需要的东西。如果想要“今天”而不是当天的全名,只需添加一个if条件来处理backcount变量为零的情况。

#2


0  

Achieving this using the DateTime Class and its format method.

使用DateTime类及其格式方法实现此目的。

The below code's output changes every day.. Since today is Sunday it starts from Monday , Tuesday... If you run this code on Tuesday , you will be getting output as Thursday , Friday , Saturday .. so on.

以下代码的输出每天都在变化。由于今天是星期日,它从星期一,星期二开始......如果你在星期二运行这段代码,你将得到周四,周五,周六的输出......等等。

<?php

for($i=1;$i<=7;$i++) //<--- Since we know total days in a week is 7.
{
    $date = new DateTime(); //<-- Grabs today's datetime
    $date->add(new DateInterval('P'.$i.'D')); //<--- Passes the current value of $i to add days..
    echo $date->format('l')."<br>";
}

OUTPUT :

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Working Demo

#1


0  

According to the PHP Manual at http://php.net/manual/en/function.date.php, just use "l" as the format parameter to get the full name of the day.

根据http://php.net/manual/en/function.date.php上的PHP手册,只需使用“l”作为格式参数即可获取当天的全名。

So 23rd Mar 2014 is a Sunday, as echoed by

因此,2014年3月23日是一个星期天

<?php
echo date ("l", mktime(0, 0, 0, 3, 23, 2014));
// Echoes Sunday
?>

To get past 7, 6, 5 or 10000 days (or number of days in the future) from the current day, according the information at this page, just use negative or positive integers in a string in the strtotime function:

要从当天开始过去7天,6天,5天或10000天(或将来的天数),根据此页面上的信息,只需在strtotime函数中的字符串中使用负整数或正整数:

<?php
$backcount = -4;
echo date ("l", strtotime("$backcount day"));
// Executed on 23 Mar 2014 will give Wednesday
?>

Knowing this, you can apply a for loop to get what you need. And if want "Today" instead of the full name of the current day, just add an if condition to handle the situation where the backcount variable is zero.

知道这一点,你可以应用for循环来获得你需要的东西。如果想要“今天”而不是当天的全名,只需添加一个if条件来处理backcount变量为零的情况。

#2


0  

Achieving this using the DateTime Class and its format method.

使用DateTime类及其格式方法实现此目的。

The below code's output changes every day.. Since today is Sunday it starts from Monday , Tuesday... If you run this code on Tuesday , you will be getting output as Thursday , Friday , Saturday .. so on.

以下代码的输出每天都在变化。由于今天是星期日,它从星期一,星期二开始......如果你在星期二运行这段代码,你将得到周四,周五,周六的输出......等等。

<?php

for($i=1;$i<=7;$i++) //<--- Since we know total days in a week is 7.
{
    $date = new DateTime(); //<-- Grabs today's datetime
    $date->add(new DateInterval('P'.$i.'D')); //<--- Passes the current value of $i to add days..
    echo $date->format('l')."<br>";
}

OUTPUT :

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Working Demo