Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

如何根据当前时间来输出向前数1-5周的日期情况?

使用循环,输出2个数组,start数组存开始时间,stop数组存截至时间。
假如今天是3月27日,则start数组是:

Array
(
    [0] => 2017-03-20
    [1] => 2017-03-13
    [2] => 2017-03-06
    [3] => 2017-02-27
)

stop数组是:

Array
(
    [0] => 2017-03-26
    [1] => 2017-03-19
    [2] => 2017-03-12
    [3] => 2017-03-05
)

如下图:
图片描述

请问如何才可以使任意在任意哪一天都可以输出以上结果?从周一开始,周日截至。我写的代码现在只是在星期一的时候生效,但是我改了系统时间为其他天数就不生效了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$today = date('N'); //今天周几

echo $mon = date('Y-m-d', strtotime("-" . ($today - 1) . "day")); //这周 周一是哪天
echo $sun = date('Y-m-d', strtotime($mon . '+ 6 days')); //这周 周日那一天

$start = [];
$stop = [];
for ($i = 0; $i < 5; $i++) {
    $start[] = $mon = date('Y-m-d', strtotime($mon . '- 7 days'));
    $stop[] = $sun = date('Y-m-d', strtotime($mon . '+ 6 days'));
}
print_r($start);
print_r($stop);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...