Как отображать воскресенье в месяц?

Я пытался получить ответ на этот вопрос, и после некоторых исследований и разработок я придумал решение

$begin = new DateTime('2014-11-01'); $end = new DateTime('2014-11-30'); $end = $end->modify('+1 day'); $interval = new DateInterval('P1D'); $daterange = new DatePeriod($begin, $interval, $end); foreach ($daterange as $date) { $sunday = date('w', strtotime($date->format("Ymd"))); if ($sunday == 0) { echo $date->format("Ymd") . "<br>"; } else { echo''; } } 

Попробуйте так:

 $begin = new DateTime('2014-11-01'); $end = new DateTime('2014-11-30'); while ($begin <= $end) // Loop will work begin to the end date { if($begin->format("D") == "Sun") //Check that the day is Sunday here { echo $begin->format("Ymd") . "<br>"; } $begin->modify('+1 day'); } 

Это еще один метод для показа всех воскресений в текущем месяце:

 <?php function getSundays($y, $m) { return new DatePeriod( new DateTime("first sunday of $y-$m"), DateInterval::createFromDateString('next sunday'), new DateTime("last day of $y-$m") ); } foreach (getSundays(2014, 11) as $sunday) { echo $sunday->format("l, Ymd\n"); } ?> 

См. Этот Codepad.viper

Вот код для воскресенья

 function getSundays($y,$m){ $date = "$y-$m-01"; $first_day = date('N',strtotime($date)); $first_day = 7 - $first_day + 1; $last_day = date('t',strtotime($date)); $days = array(); for($i=$first_day; $i<=$last_day; $i=$i+7 ){ $days[] = $i; } return $days; } $days = getSundays(2016,04); print_r($days); 

Попробуйте это, чтобы найти воскресенья текущего месяца.

 $month = date('m'); $year = date('Y'); $days = cal_days_in_month(CAL_GREGORIAN, $month,$year); for($i = 1; $i<= $days; $i++){ $day = date('Ym-'.$i); $result = date("l", strtotime($day)); if($result == "Sunday"){ echo date("Ymd", strtotime($day)). " ".$result."<br>"; } }