如何获得两个日期之间的星期日

vis*_*huB 0 php

我试试这个

<?php
    $startdate = '2016-07-15';
    $enddate = '2016-07-17';
    $sundays = [];
    $startweek=date("W",strtotime($startdate));
    $endweek=date("W",strtotime($enddate));
    $year=date("Y",strtotime($startdate));

    for($i=$startweek;$i<=$endweek;$i++) {
        $result=$this->getWeek($i,$year);
        if($result>$startdate && $result<$enddate) {
            $sundays[] = $result;
        }
    }
    print_r($sundays);

    public function getWeek($week, $year)
    {
       $dto = new \DateTime();
       $result = $dto->setISODate($year, $week, 0)->format('Y-m-d');
       return $result;
    }
?>
Run Code Online (Sandbox Code Playgroud)

这个返回空白数组.但在两个日期之间2016-07-17是星期天.

我输出为 2016-07-17

在这里提到这个 但是在这个链接中返回输出为星期日没有日期.

Mat*_*hai 8

尝试一下:

$startDate = new DateTime('2016-07-15');
$endDate = new DateTime('2016-07-17');

$sundays = array();

while ($startDate <= $endDate) {
    if ($startDate->format('w') == 0) {
        $sundays[] = $startDate->format('Y-m-d');
    }

    $startDate->modify('+1 day');
}

var_dump($sundays);
Run Code Online (Sandbox Code Playgroud)