我想总结一天不重复的时间,请看下面链接中的图片以便更好地了解
http://postimage.org/image/ew50dnlw/
总时间 5小时
我不想计算黄色区域的两倍,因为另一个时间已经填满
谢谢!
答案 0 :(得分:1)
在上面的回答中使用Andre提出的想法我可以用下面的代码实现目标: 注意:为了使其正常工作,请不要忘记按开始时间ASC
订购时间<?php
// Function to sum times
function sum($time1, $time2) {
$times = array($time1, $time2);
$seconds = 0;
foreach ($times as $time) {
list($hour, $minute, $second) = explode(':', $time);
$seconds += $hour * 3600;
$seconds += $minute * 60;
$seconds += $second;
}
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
// Function to subtract times
function sub($end, $start) {
$end = strtotime($end);
$start = strtotime($start);
$seconds = $end - $start;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
}
// Some values to work on
// To get this working the times should be ordered by start time ASC
$start = array(
'11:00:00',
'13:00:00',
'14:00:00',
'15:00:00',
);
$end = array(
'12:30:00',
'14:30:00',
'16:00:00',
'16:30:00',
);
$total = "00:00:00";
$tmp = "00:00:00";
for ($i = 0; $i <= count($start); $i++) {
$start_t = $start[$i];
$end_t = $end[$i];
if ($start_t >= $tmp) {
$total = sum(sub($end_t, $start_t), $total);
$tmp = $end_t;
} else {
if ($end_t > $tmp) {
$total = sum(sub($end_t, $tmp), $total);
$tmp = $end_t;
} else {
}
}
}
echo $total;
答案 1 :(得分:0)
我做了类似的事情,但是使用Telerik的RadScheduler,所以我不能为你提供代码,而是描述逻辑。
我假设您已经在数组中预约了这些约会,按开始日期排序
首先运行:
后续运行:
请提供您的代码,以便我们更好地帮助您