Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算轮班工作时间并检测_Php_Date_Time_Clock - Fatal编程技术网

Php 计算轮班工作时间并检测

Php 计算轮班工作时间并检测,php,date,time,clock,Php,Date,Time,Clock,我有个问题。我的英语很差。我需要用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) { re

我有个问题。我的英语很差。我需要用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您用于tip##u start&tip####u end的
strottime
不考虑您在示例中使用的日期,它们采用当前服务器日期。。我也不太明白你想要达到的目的背后的逻辑。对不起。我知道我不能,非常感谢你的帮助。也许这张照片给了我比我更多的细节。好的,这张图片只说明了你上面提供的
错误的
脚本答案。。。我正试图找出正确计算的逻辑或数学。。。