如何通过给出开始日期来获得接下来的10个星期天


How to get next 10 sundays by giving start date.?

我如何通过只提供当前数据来获得接下来的10个星期日,

以下是我的代码,但它在日期范围之间,我只想给出开始日期,并从开始日期起获得接下来的10个星期日(没有结束日期)。

    $allweeks=array();
    $startDate = date('Y-m-d');
    $date = new DateTime($startDate);
    $interval = new DateInterval('P2M');
    $date->add($interval);
    $endDate=$date->format('Y-m-d');
    for ($i = strtotime($startDate); $i <= strtotime($endDate); $i = strtotime('+1 day', $i)) {
      if (date('N', $i) == 1)  
         $allweeks[]= date('n-j', $i);
    }
    foreach($allweeks as $wks)
    {
        $weeks=explode('-', $wks);
        echo '=Week of '.$weeks[0]."/".$weeks[1];
    }

您可以使用DateTime类或strtotime函数进行相同操作:

看看下面的代码:

使用DateTime

$date = '2016-04-05';
$date = new DateTime($date);
// Modify the date it contains
for($i =0; $i < 10; $i++){
$date->modify('next sunday');
// Output
echo $date->format('l : Y-m-d');
echo '<br />';
}

使用strtotime函数

$date = '2016-04-05';
// Modify the date it contains
for($i =0; $i < 10; $i++){
    $date = date('Y-m-d', strtotime('next sunday', strtotime($date)));
    echo date('l : Y-m-d', strtotime($date));
    echo '<br />';
}

输出

Sunday : 2016-04-10
Sunday : 2016-04-17
Sunday : 2016-04-24
Sunday : 2016-05-01
Sunday : 2016-05-08
Sunday : 2016-05-15
Sunday : 2016-05-22
Sunday : 2016-05-29
Sunday : 2016-06-05
Sunday : 2016-06-12