Teşekkürler Harmen, kodunuzu bana doğru yönde işaret etti. Çok takdir!
Sizin çözüm timestamp1 bir Cuma olduğunda dikkate almak, => yerine "önümüzdeki Cuma" ve "bu Cuma" olmalı değil
Ben damgaları tek, birden çok hafta sonları değil kapsayacak böylece genişletilmiş.
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;
}