How to calculate the total break time between two dates in PHP
Calculating Break Time with PHP
In this blog post, we’ll explore a PHP script that calculates the total break time within a given time period, considering multiple break periods. This can be useful in a variety of scenarios, such as tracking work hours, scheduling tasks, or even planning your day.
The Function
The function calculateBreakTime
takes three parameters: $startDateTime
, $stopDateTime
, and $breakTimeArray
. The first two are DateTime objects representing the start and end of the time period. The third is an array of break periods, each defined as a pair of DateTime objects.
Here’s how it works:
The function initializes $totalBreakTime
to 0. This variable will hold the total duration of all break periods that fall within the given time period.
The function then enters a loop that iterates over each day in the given time period.
For each day, it loops over each break period in $breakTimeArray
. For each break period, it adjusts the start and stop times to the current date.
It then checks if there is any overlap between the current day and the break period. If there is, it calculates the duration of the overlap in seconds and adds it to $totalBreakTime
.
After all days and break periods have been processed, the function converts $totalBreakTime
from seconds to hours and minutes, and prints out the result.
Example Usage
In this example, we’ve defined two break periods: from 1:00 pm to 2:00 pm, and from 4:00 pm to 7:00 pm. We’re calculating the total break time between ‘2023-08-24 09:36 am’ and ‘2023-09-24 1:30 pm’. The script will output the total duration of all breaks that fall within this period.
<?php
function calculateBreakTime($startDateTime, $stopDateTime, $breakTimeArray) {
$totalBreakTime = 0;
for ($date = clone $startDateTime; $date <= $stopDateTime; $date->modify('+1 day')) {
foreach ($breakTimeArray as $break) {
list($breakStart, $breakStop) = $break;
// Adjust break start/stop to current date
$breakStart->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
$breakStop->setDate($date->format('Y'), $date->format('m'), $date->format('d'));
// Calculate overlap with the day
if ($startDateTime < $breakStop && $stopDateTime > $breakStart) {
// Overlap exists
$overlapStart = max($startDateTime, $breakStart);
$overlapStop = min($stopDateTime, $breakStop);
// Add overlap to total break time
$totalBreakTime += max(0, $overlapStop->getTimestamp() - $overlapStart->getTimestamp());
}
}
}
// Convert total break time to hours and minutes and print it out
echo "Total break time: " . floor($totalBreakTime / 3600) . " hours " . (($totalBreakTime / 60) % 60) . " minutes\n";
}
// Example usage:
$startDateTime = new DateTime('2023-08-24 09:36 am');
$stopDateTime = new DateTime('2023-09-24 1:30 pm');
// break1
$break1Start = new DateTime('13:00');
$break1Stop = new DateTime('14:00');
// break2
$break2Start = new DateTime('16:00');
$break2Stop = new DateTime('19:00');
$breakTimeArray = [
[$break1Start, $break1Stop],
[$break2Start, $break2Stop],
];
calculateBreakTime($startDateTime, $stopDateTime, $breakTimeArray);
?>
This script is flexible and can be adapted to different scenarios by adjusting the input parameters. Whether you’re tracking work hours or planning your day, this script provides a simple and effective way to calculate break times.
Leave A Comment