Reputation: 1
Given 2 dates, how can i calculate the number of hours/minutes between them, excluding weekend time (since Friday 17:30 till Monday 09:00)
The script queries a MySql database but i assume this sort of calculations would be easier in php. I'm thinking of a iterative function to determine the weekday and keep looping until last date would be smaller than current day indicator?
Any suggestions?
Upvotes: 0
Views: 2499
Reputation: 59
Thanks Harmen, your code pointed me in the right direction. Much appreciated!!
Your solution does not take into consideration when timestamp1 is in a friday, => instead of "next friday" should be "this friday"
I expanded so that timestamps can cover multiple weekends, not just one.
function timeDiffWeekendsOff($timestamp1, $timestamp2){
// Double check $timestamp2 is after and not before $timestamp1
if ($timestamp2 < $timestamp1) return 0;
// All-time difference
$difference = $timestamp2 - $timestamp1;
// This will hold the number of weekend-seconds that needs to be subtracted
$weekendSubtract = 0;
$keepLoop = true;
$currentDayStart = $timestamp1;
while ($keepLoop) {
if (isSameDay($currentDayStart,$timestamp2)){
$keepLoop = false; // exit at the last day
$currentDayEnd = $timestamp2;
} else {
$currentDayEnd = strtotime('tomorrow 00:00', $currentDayStart);
}
switch (date('w',$currentDayStart)){ // 0 - Sunday, 1 - Monday, 5 - Friday, 6 - Saturday
case 5: // Friday
$weekendSubtract += timeIntersect($currentDayStart, $currentDayEnd, strtotime('this Friday 17:30', $currentDayStart), strtotime('this Saturday 00:00', $currentDayStart));
break;
case 6: // full weekend days, 0 - Sunday, 6 - Saturday
case 0:
$weekendSubtract += $currentDayEnd - $currentDayStart;
break;
case 1: // Monday
$weekendSubtract += timeIntersect($currentDayStart, $currentDayEnd, strtotime('this Monday 00:00', $currentDayStart), strtotime('this Monday 09:00', $currentDayStart));
default:
break;
} // end switch
// -- lastly, set the new day start
$currentDayStart = strtotime('tomorrow 00:00', $currentDayStart);
} // end while $keepLoop
$difference -= $weekendSubtract;
return $difference;
}
function isSameDay($compare1, $compare2){
if (date('Ymd',$compare1)==date('Ymd',$compare2))
return true;
else
return false;
}
function timeIntersect($time1_from, $time1_to, $time2_from, $time2_to){
$overlap = min($time1_to, $time2_to) - max($time1_from, $time2_from);
if ($overlap < 0) $overlap = 0;
return $overlap;
}
Upvotes: 0
Reputation: 22438
Assuming that you have two timestamps: $timestamp1
and $timestamp2
, you could just subtract them and subtract the number of weekends: $difference - ($weekends*2*24*3600)
.
function difWeekends($timestamp1, $timestamp2){
$difference = $timestamp2 - $timestamp1;
$nextWeekend = strtotime('next Saturday', $timestamp1);
$weekends = 0;
$weekendFound = true;
while($weekendFound){
if($nextWeekend < $timestamp2){
$weekendFound = false;
} else {
$nextWeekend = strtotime('+7 days', $nextWeekend);
$weekends++;
}
}
$difference -= $weekends*48*60*60;
$hours = $difference % 3600; // Not sure about this, I assume you already got this...
$difference -= $hours*3600;
$minutes = $difference % 60;
return array('hours'=>$hours, 'minutes'=>$minutes);
}
Edit was needed:
function difWeekends($timestamp1, $timestamp2){
$difference = $timestamp2 - $timestamp1;
// Calculate the start of the first next weekend
$nextWeekendStart = strtotime('next Friday 17:30', $timestamp1);
// Calculate the end of the first next weekend
$nextWeekendEnd = strtotime('next Monday 9:00', $nextWeekendStart);
// This wil hold the number of weekend-seconds that needs to be subtracted
$weekendSubtract = 0;
$weekendFound = true;
while($weekendFound){
// If $timestamp2 ends before the next weekend, no weekend is found. [] = weekends, --- is time range
// --- [ ]
if($timestamp2 < $nextWeekendStart){
$weekendFound = false;
} else {
// ----[-- ]
if($timestamp2 < $nextWeekendEnd){
$weekendSubtract += $timestamp2-$nextWeekendStart;
$weekendFound = false; // Last weekend was found
} else {
// ---[----]---
$weekendSubtract += $nextWeekendEnd - $nextWeekendStart;
$nextWeekendStart += 7*24*60*60;
$nextWeekendEnd += 7*24*60*60;
}
}
}
$difference -= $weekendSubtract;
$hours = $difference % 3600; // Not sure about this, I assume you already got this...
$difference -= $hours*3600;
$minutes = $difference % 60;
return array('hours'=>$hours, 'minutes'=>$minutes);
}
Upvotes: 1