从PHP的特定日期获取上一个和接下来的7天? [英] Getting the previous and next 7 days from a specific date in PHP?
本文介绍了从PHP的特定日期获取上一个和接下来的7天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从特定日期获取上一个和接下来的7天
Getting the previous and next 7 days from a specific date
$date = new DateTime();
$today = $date->format('m d, Y');
如何从$ today获得之前的7天及以后的7天?并将其循环在一个数组中。
How can I get the previous 7 and next 7 days from $today? and loop them in an array.
推荐答案
与其他答案不同的是什么,但是如何
Something different to the other answers but what about
$start = new DateTime();
$end = new DateTime();
$start = $start->modify( '-7 days' );
$end = $end->modify( '+8 days' ); // Date Period doesn't include the end date
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start, $interval ,$end);
foreach($daterange as $date){
echo $date->format("'m d, Y'") . "<br>";
}
这篇关于从PHP的特定日期获取上一个和接下来的7天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文