Here's a PHP function that receives two date strings and returns the number of weekend days (Saturday or Sunday) that occur within that date range:
function countWeekendDays($startDate, $endDate) {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = new DateInterval('P1D'); // 1 day interval
$period = new DatePeriod($start, $interval, $end);
$weekendDays = 0;
foreach ($period as $date) {
$dayOfWeek = $date->format('N'); // 1 (Monday) through 7 (Sunday)
if ($dayOfWeek == 6 || $dayOfWeek == 7) {
$weekendDays++;
}
}
return $weekendDays;
}
Explanation:
The function first creates DateTime objects for the start and end dates using the DateTime constructor.
It then creates a DateInterval object for a 1 day interval, and a DatePeriod object with the start date, interval, and end date as parameters. This allows us to loop through each day in the date range.
For each day in the DatePeriod, we get its day of the week using the format method with the 'N' format character. This returns a number from 1 (Monday) to 7 (Sunday).
If the day of the week is 6 or 7 (Saturday or Sunday), we increment the weekendDays counter.
Finally, we return the weekendDays counter.
To optimize this function, we can use the fact that weekends occur on a regular pattern: every 7 days. We can calculate the number of full weekend days in the date range by dividing the number of days by 7 and rounding down, then multiplying by 2. We then check if the start or end date falls on a weekend day, and add 1 to the result for each one that does.
Here's the optimized function:
function countWeekendDays($startDate, $endDate) {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
$days = floor(($end->getTimestamp() - $start->getTimestamp()) / (60 * 60 * 24)) + 1; // add 1 to include both start and end dates
$weekends = floor($days / 7) * 2;
$startDayOfWeek = $start->format('N');
$endDayOfWeek = $end->format('N');
if ($startDayOfWeek > 5) { // Saturday or Sunday
$weekends--;
}
if ($endDayOfWeek > 5) { // Saturday or Sunday
$weekends--;
}
return $weekends;
}
Explanation:
The function first calculates the number of days in the date range by getting the difference between the start and end dates in seconds, dividing by the number of seconds in a day, and adding 1 to include both the start and end dates. We use the getTimestamp method to get the Unix timestamp of each date, which makes the calculation simple.
We then calculate the number of full weekends in the date range by dividing the number of days by 7, rounding down, and multiplying by 2. This works because there are two weekend days per 7-day period.
We then check if the start or end date falls on a weekend day by getting their day of the week using the format method with the 'N' format character, and checking if it's greater than 5 (Saturday or Sunday).