У меня проблема. Я очень плохо говорю по-английски. Мне нужно выполнить расчет сверхурочной работы с помощью PHP. Для этого уже есть код. Но при времени работы более 2-х суток расчет неверный.
Старт работы: 2018-09-09 13:43
Рабочий конец: 2018-09-11 07:13
Результат: 07:18 | 04:00 | 04:00 | 02:00 | 17:18
Я не могу разобраться в проблеме, помогите пожалуйста. Я хотел бы рассказать вам поподробнее, но я очень плохой. Я надеюсь, вы меня понимаете.
Спасибо.
<?php
function intersection($s1, $e1, $s2, $e2,$day) {
return subintersection($s1,$e1,$s2,$e2)+subintersection($s1,$e1,$s2+$day,$e2+$day);
}
function subintersection($s1, $e1, $s2, $e2) {
if ($e1 < $s2)
return 0;
if ($s1 > $e2)
return 0;
if ($s1 < $s2)
$s1 = $s2;
if ($e1 > $e2)
$e1 = $e2;
return $e1 - $s1;
}
function minuteToHours($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
function calculateWork($strstart, $strend, $arg = NULL){
$day= 3600*24;
$strS = strtotime($strstart);
$strE = strtotime($strend);
$date_start = date('Y-m-d', $strS);
$date_end = date('Y-m-d', $strE);
$hour_start = date('H:i', $strS);
$hour_end = date('H:i', $strE);
$start = strtotime($hour_start);
$end = strtotime($hour_end);
if ($date_start != $date_end){
$end = $end + 3600*24;
}
$tip1_start = strtotime("06:00");
$tip1_end = strtotime("20:00");
$tip2_start = strtotime("20:00");
$tip2_end = strtotime("00:00") + 3600*24;
$tip3_start = strtotime("00:00");
$tip3_end = strtotime("04:00");
$tip4_start = strtotime("04:00");
$tip4_end = strtotime("06:00");
$tip1 = intersection( $start, $end, $tip1_start, $tip1_end, $day) / 60;
$tip2 = intersection( $start, $end, $tip2_start, $tip2_end, $day) / 60;
$tip3 = intersection( $start, $end, $tip3_start, $tip3_end, $day) / 60;
$tip4 = intersection( $start, $end, $tip4_start, $tip4_end, $day) / 60;
if (null !== minuteToHours($tip1, '%02d:%02d')){
$t1 = minuteToHours($tip1, '%02d:%02d');
}
if (null !== minuteToHours($tip2, '%02d:%02d')){
$t2 = minuteToHours($tip2, '%02d:%02d');
}
if (null !== minuteToHours($tip3, '%02d:%02d')){
$t3 = minuteToHours($tip3, '%02d:%02d');
}
if (null !== minuteToHours($tip4, '%02d:%02d')){
$t4 = minuteToHours($tip4, '%02d:%02d');
}
$topla = $tip1 + $tip2 + $tip3 + $tip4;
$total = minuteToHours($topla, '%02d:%02d');
return "{$t1}|{$t2}|{$t3}|{$t4}|{$total}";
}
echo calculateWork("2018-09-09 13:43", "2018-09-11 07:01");
//07:18|04:00|04:00|02:00|17:18
Результат: 21: 18 | 08: 00 | 08: 00 | 04: 00 | 41: 18
strtotime
, который вы используете для tip # _start & tip # _end, не принимает во внимание даты, которые вы используете в своем примере, они принимают текущую дату сервера .. Также я действительно не понимаю логики того, что вы пытаетесь выполнить. .
Извините. Я знаю, что не могу, и большое вам спасибо за помощь. Возможно, эта фотография дает мне больше деталей, чем я. https://image.ibb.co/iM6CWV/image.png
На картинке указано только то, что вы предоставили выше как ответ wrong
вашего скрипта ... Я пытаюсь выяснить ЛОГИКУ или математику для правильного расчета ...
Почему ответ не 21:30|08:00|08:00|04:00|41:30
дан, что 41:30
находится между 2018-09-09 13:43
и 2018-09-11 07:13
?
Извините, я не совсем понимаю вопрос?
каков будет ожидаемый правильный результат из приведенного выше примера?