To find number of days in every week between two given date ranges in PHP, the code is as follows −
Example
<?php
$start = "11-11-2019";
$end = "12-12-2019";
$week_day = array('Monday' => 0,
'Tuesday' => 0,
'Wednesday' => 0,
'Thursday' => 0,
'Friday' => 0,
'Saturday' => 0,
'Sunday' => 0);
$start = new DateTime($start);
$end = new DateTime($end);
while($start <= $end )
{
$time_stamp = strtotime($start->format('d-m-Y'));
$week = date('l', $time_stamp);
$week_day[$week] = $week_day[$week] + 1;
$start->modify('+1 day');
}
print_r("The number of days between the given range is");
print_r($week_day);
?>Output
The number of days between the given range isArray ( [Monday] => 5 [Tuesday] => 5 [Wednesday] => 5 [Thursday] => 5 [Friday] => 4 [Saturday] => 4 [Sunday] => 4 )
Two dates of ‘DateTime’ type is defined and an array of days of a week is defined, where initially the count of all days of a week are 0. The dates are converted to a time format and timestamp variable is assigned to it. An array named ‘week_day’ is incremented and increments the days of the week based on how many times it has been encountered during the iteration.