我有时间间隔如下,
1- 07:00:00 to 09:30:00 (duration 02:30:00)
2- 08:00:00 to 11:00:00 (duration 03:00:00)
但实际的综合持续时间是07:00-11:00(杜尔04:00:00)。当我们添加两个单独的持续时间时,它将给出结果5:30:00..like,这种不同的组合可能会发生(1-7,5-8,9-10,6-9:30),我想要一个逻辑来找到实际的持续时间,而不考虑相交的时间间隔。如果它已经被回答了,请给我一条通往它的路。提前谢谢。注:它是根据出勤数据和值勤单计算工作时间。
发布于 2015-03-17 14:04:32
enter code here
function compareTimes($start,$end)
{
$count=count($start);
for($i=0;$i<$count;$i++)
{
for($j=0;$j<$count;$j++)
{
if($j==$i)
{
}
elseif($start[$j]>=$start[$i] and $start[$j]<=$end[$i])
{
if($end[$j]>=$start[$i] and $end[$j]<=$end[$i])
{
$start[$j]="0";
$end[$j]="0";
}
else
{
$end[$i]=$end[$j];
$start[$j]="0";
$end[$j]="0";
}
}
}
}
$dur=0;
$count1=count($start);
for($i=0;$i<$count1;$i++)
{
$dur1=$end[$i]-$start[$i];
$dur=$dur1+$dur;
}
return $dur;
}
//例为5-6、4-8和8-9
$start=array("5","4","8");
$end=array("6","8","9");
$check=compareTimes($start,$end);
发布于 2015-03-17 09:21:16
我不知道有什么自动的方法可以做到这一点,但IMO最好的方法是将它们转换为整数(Unix时间戳),然后一次处理两个。然后,您可以找到最早的开始时间和最近的结束时间来计算订单,然后检查是否前面的结束时间与最新的开始时间重叠。
function compareTimes($start1, $end1, $start2, $end2)
{
$earliest = $start1 <= $start2 ? 1 : 2;
$latest = $end1 >= $end2 ? 1 : 2;
if ($earliest == $latest)
{
$end = 'end'.$latest;
$start = 'start'.$earliest;
return ($$end - $$start);
}
// Check to see if there is any overlap.
$earlyEnd = 'end'.$earliest;
$lateStart = 'start'.$latest;
if ($earlyEnd >= $lateStart)
{
$start = 'start'.$earliest;
$end = 'end'.$latest;
return ($$end - $$start);
}
// If there is no overlap we just total the duration of the two.
return (($end1 - $start1) + ($end2 - $start2));
}
$start1 = strtotime('01/01/1970 07:00');
$end1 = strtotime('01/01/1970 09:30');
$start2 = strtotime('01/01/1970 08:00');
$end2 = strtotime('01/01/1970 11:00');
$totalDuration = compareTimes($start1, $end1, $start2, $end2);
注:我还没有测试过这段代码。在生活环境中使用之前,请先检查一下。
https://stackoverflow.com/questions/29094974
复制相似问题