I want to calculate all Sunday's minutes between two given dates.
function getWeekEnd($startDate, $endDate)
{
$working_hours = [
[0, 86400], // Sun
null,
null,
null,
null,
null,
null //Sat
];
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$seconds = 0; // Total working seconds
// Calculate the Start Date (Midnight) and Time (Seconds into day) as Integers.
$start_date = clone $start;
$start_date = $start_date->setTime(0, 0, 0)->getTimestamp();
$start_time = $start->getTimestamp() - $start_date;
// Calculate the Finish Date (Midnight) and Time (Seconds into day) as Integers.
$end_date = clone $end;
$end_date = $end_date->setTime(0, 0, 0)->getTimestamp();
$end_time = $end->getTimestamp() - $end_date;
// For each Day
for ($today = $start_date; $today <= $end_date; $today += 86400) {
// Get the current Weekday.
$today_weekday = date('w', $today);
// Skip to next day if no hours set for weekday.
if (!isset($working_hours[$today_weekday][0]) || !isset($working_hours[$today_weekday][1])) continue;
// Set the office hours start/finish.
$today_start = $working_hours[$today_weekday][0];
$today_end = $working_hours[$today_weekday][1];
// Adjust Start/Finish times on Start/Finish Day.
if ($today === $start_date) $today_start = min($today_end, max($today_start, $start_time));
if ($today === $end_date) $today_end = max($today_start, min($today_end, $end_time));
// Add to total seconds.
$seconds += $today_end - $today_start;
}
$time = date('H:i', $seconds);
$hms = explode(":", $time);
return ($hms[0] + ($hms[1]/60));
}
For the moment i have this but if i make 2019-11-22 22:00:00 to 2019-11-28 10:00:00 i have no return do you have another function or fix this function ?
Thank a lot