2

I try this

<?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;
    }
?>

this return blank array. but in between two dates 2016-07-17 is Sunday.

I get output as 2016-07-17

I refer this here But in this link return output as no of sunday not date.

Community
  • 1
  • 1
vishuB
  • 4,173
  • 5
  • 31
  • 49

5 Answers5

9

Give this a try:

$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);

If you want later to use the DateTime objects instead of the formatted date, then you must use DateTimeImmutable for the $startDate variable:

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

$sundays = array();

while ($startDate <= $endDate) {
    if ($startDate->format('w') == 0) {
        $sundays[] = $startDate;
    }
    
    $startDate->modify('+1 day');
}

var_dump($sundays);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
1
function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
 $startDate = strtotime($startDate);
 $endDate = strtotime($endDate);

$dateArr = array();

do
{
    if(date("w", $startDate) != $weekdayNumber)
    {
        $startDate += (24 * 3600); // add 1 day
    }
} while(date("w", $startDate) != $weekdayNumber);


while($startDate <= $endDate)
{
    $dateArr[] = date('Y-m-d', $startDate);
    $startDate += (7 * 24 * 3600); // add 7 days
}

return($dateArr);
}
  $dateArr = getDateForSpecificDayBetweenDates('2010-01-01', '2010-12-31', 0);
  print "<pre>";
  print_r($dateArr);

Try out this code..

Dipika
  • 115
  • 1
  • 3
  • 15
0

Try this

$start = new DateTime($startDate);
        $end = new DateTime($endDate);

        $sundays = [];
        while ($start->getTimestamp() != $end->getTimestamp()) {
            if ($start->format('w') == 0) {
                $sundays[] = $start->format('Y-m-d');
            }
            $start->add('+1 DAY');
        }
vijaykumar
  • 4,658
  • 6
  • 37
  • 54
0

This will return you all sundays between two dates.

$startdate = '2016-05-1';
$enddate   = '2016-05-20';

function getSundays($start, $end) {
    $timestamp1 = strtotime($start);
    $timestamp2 = strtotime($end);
    $sundays    = array();
    $oneDay     = 60*60*24;

    for($i = $timestamp1; $i <= $timestamp2; $i += $oneDay) {
        $day = date('N', $i);

        // If sunday
        if($day == 7) {
            // Save sunday in format YYYY-MM-DD, if you need just timestamp
            // save only $i
            $sundays[] = date('Y-m-d', $i);

            // Since we know it is sunday, we can simply skip 
            // next 6 days so we get right to next sunday
            $i += 6 * $oneDay;
        }
    }

    return $sundays;
}


var_dump(getSundays($startdate, $enddate));
Buksy
  • 11,571
  • 9
  • 62
  • 69
0

Use Carbon

$arrayOfDate = [];
$startDate = Carbon::parse($startDate)->modify('this sunday');
$endDate = Carbon::parse($endDate);

for ($date = $startDate; $date->lte($endDate); $date->addWeek()) {
    $arrayOfDate[] = $date->format('Y-m-d');
}

return $arrayOfDate;
Kokil
  • 560
  • 1
  • 5
  • 16