2011-05-03 59 views
0

時間更復雜的和我想總結只是沒有在一天重複多次,看到鏈接下面的圖片,以更好地瞭解在PHP或學說

http://postimage.org/image/ew50dnlw/

的總時間應該5小時

我不想因爲其他時間已經填補了時間

感謝計兩次黃色區域!

+0

,如果你想人來幫助你 – 2011-05-03 13:20:52

+0

你有什麼[試圖]已經在這裏粘貼一段代碼? – 2011-05-03 13:21:24

回答

0

我做了類似的事情,但使用Telerik的RadScheduler,所以我不能提供你的代碼,但描述了邏輯。

我假設你已經有一個數組的約會,按開始日期

首先運行命令:

  1. 獲得第一個任命
  2. 讀取開始時間
  3. 閱讀結束時間
  4. 與變量的總和差

後續運行:

  1. 獲取下一個約會
  2. 讀取開始時間
  3. 閱讀結束時間
  4. 與 先前的結束日期
  5. 如果是小的比較這個起始日期(他們插值) 此結束時間之間的總差額 與上次結束時間
  6. 其他(不插入),總差異下注吐溫這 結束時間與此開始時間

請提供你的代碼,這樣我就可以幫助您更好地

+0

但是如果我的後續運行在其開始和結束內部有兩次?如果我在比較他們之前以ASC開始時間排序,可能會有幫助,您怎麼看? – Marcelo 2011-05-03 14:12:47

+0

你是對的,你也必須比較結束時間。關於按開始時間排序,我在我的回答中已經說過。 – Andre 2011-05-03 14:14:24

+0

對不起,我沒有看到。 我會在這裏嘗試一些東西,使用你的想法 只需一分鐘 – Marcelo 2011-05-03 14:25:56

1

使用由安德烈提出的想法在上面的回答,我可以用下面的代碼實現的目標: 注意:要得到這個工作正常,不要忘記訂購您倍起始時間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;