php 获取下周一、周二等的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1188728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:26:13  来源:igfitidea点击:

Get the date of next monday, tuesday, etc

phpdatetime

提问by Adrian Sarli

I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!

我想找到星期一、星期二、星期三等的日期戳。如果这周还没到,我希望日期是本周,否则,下周。谢谢!

回答by Tom Haigh

See strtotime()

strtotime()

strtotime('next tuesday');

You could probably find out if you have gone past that day by looking at the week number:

通过查看周数,您可能会发现自己是否已经过了那一天:

$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);

if ($weekNoNextTuesday != $weekNo) {
    //past tuesday
}

回答by Adrian Sarli

I know it's a bit of a late answer but I would like to add my answer for future references.

我知道这有点晚了,但我想添加我的答案以供将来参考。

// Create a new DateTime object
$date = new DateTime();

// Modify the date it contains
$date->modify('next monday');

// Output
echo $date->format('Y-m-d');

The nice thing is that you can also do this with dates other than today:

好消息是您还可以使用今天以外的日期执行此操作:

// Create a new DateTime object
$date = new DateTime('2006-05-20');

// Modify the date it contains
$date->modify('next monday');

// Output
echo $date->format('Y-m-d');

To make the range:

要制作范围:

$monday = new DateTime('monday');

// clone start date
$endDate = clone $monday;

// Add 7 days to start date
$endDate->modify('+7 days');

// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');

$dateRange = new DatePeriod($monday, $dateInterval, $endDate);

foreach ($dateRange as $day) {
    echo $day->format('Y-m-d')."<br />";
}

References

参考

PHP Manual - DateTime

PHP 手册 -日期时间

PHP Manual - DateInterval

PHP 手册 - 日期间隔

PHP Manual - DatePeriod

PHP 手册 - DatePeriod

PHP Manual - clone

PHP 手册 -克隆

回答by Josh Davis

The question is tagged "php" so as Tom said, the way to do that would look like this:

正如汤姆所说,这个问题被标记为“php”,这样做的方法是这样的:

date('Y-m-d', strtotime('next tuesday'));

回答by Farid Movsumov

You can use Carbonlibrary.

您可以使用库。

Example: Next week friday

示例:下周星期五

Carbon::parse("friday next week");

回答by Oksana Romaniv

PHP 7.1:

PHP 7.1:

$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();

PHP manual getTimestamp()

PHP 手册getTimestamp()

回答by Larry Watanabe

Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:

抱歉,我没有注意到 PHP 标签 - 但是其他人可能对 VB 解决方案感兴趣:

Module Module1

    Sub Main()
        Dim d As Date = Now
        Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
        Console.WriteLine("next friday is " & nextFriday)
        Console.ReadLine()
    End Sub

End Module

回答by Eann

if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.

如果你想找到星期一,那么 'dayOfWeek' 是 1,如果是星期二,它将是 2,依此类推。

var date=new Date();
getNextDayOfWeek(date, 2);

// this is for finding next tuesday

// 这是为了寻找下周二

function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)

var resultDate = new Date(date.getTime());

resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);

return resultDate;
}

Hope this will be helpfull to you, thank you

希望这对你有帮助,谢谢

回答by Mike

If I understand you correctly, you want the dates of the next 7 days?

如果我理解正确,您想要接下来 7 天的日期吗?

You could do the following:

您可以执行以下操作:

for ($i = 0; $i < 7; $i++)  
  echo date('d/m/y', time() + 86400 * $i);

Check the documentation for the date function for the format you want it in.

检查日期函数的文档以获取您想要的格式。

回答by Denimar Fernandez

For some reason, strtotime('next friday')display the Friday date of the current week. Try this instead:

出于某种原因,strtotime('next friday')显示当前周的星期五日期。试试这个:

//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14

$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07

回答by Dexygen

The PHP documentation for time()shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.

time()PHP 文档显示了一个示例,说明如何获取一周外的日期。您可以将其修改为进入最多迭代 7 次的循环,每次获取时间戳,获取相应的日期,并从中获取星期几。