2014-10-31 64 views
1

希望有人可以幫助我在這裏。 我有2個給定的開始和結束時間的數組。 我需要做的是,以檢查是否從陣列1的時間範圍是在陣列2的範圍,如果它是在其範圍內必須從陣列中移除2.PHP比較2時間範圍的陣列

陣列1:

Array ( 
     [0] => Array ([from] => 10:00:00 [to] => 10:30:00) 
     [1] => Array ([from] => 11:00:00 [to] => 11:45:00) 
     [2] => Array ([from] => 14:00:00 [to] => 15:00:00) 
     [3] => Array ([from] => 16:30:00 [to] => 17:00:00) 
) 

陣列2:

Array ( 
     [0] => Array ([from] => 09:30 [to] => 10:00) 
     [1] => Array ([from] => 10:15 [to] => 10:45) 
     [2] => Array ([from] => 11:00 [to] => 11:30) 
     [3] => Array ([from] => 11:45 [to] => 12:15) 
     [4] => Array ([from] => 12:30 [to] => 13:00) 
     [5] => Array ([from] => 13:15 [to] => 13:45) 
     [6] => Array ([from] => 14:00 [to] => 14:30) 
     [7] => Array ([from] => 14:45 [to] => 15:15) 
     [8] => Array ([from] => 15:30 [to] => 16:00) 
     [9] => Array ([from] => 16:15 [to] => 16:45) 
     [10] => Array ([from] => 17:00 [to] => 17:30) 
     [11] => Array ([from] => 17:45 [to] => 18:15) 
     [12] => Array ([from] => 18:30 [to] => 19:00) 
     [13] => Array ([from] => 19:15 [to] => 19:45) 
) 

希望有某人那裏誰可以幫助我。 在此先感謝您的幫助!

+0

你試過了什麼? – Xlander 2014-10-31 06:40:05

回答

1

PS:編輯爲OP提供了更多的細節。

這應該是一個評論,但:o「聲譽!」你讓這個答案!

你的問題是不完整和模糊的! (後面增加更多信息)

只需檢查每個可用的時隙與保留的時隙!如果可用時隙仍然「可用」,則將其添加到臨時陣列,最後更新可用時隙的陣列:

<?php 
$array1 = Array (
0 => Array ('from' => '10:00:00', 'to' => '10:30:00'), 
1 => Array ('from' => '11:00:00', 'to' => '11:45:00'), 
2 => Array ('from' => '14:00:00', 'to' => '15:00:00'), 
3 => Array ('from' => '16:30:00', 'to' => '17:00:00')); 

$array2 = Array(
0 => Array ('from' => '09:30', 'to' => '10:00'), 
1 => Array ('from' => '10:15', 'to' => '10:45'), 
2 => Array ('from' => '11:00', 'to' => '11:30'), 
3 => Array ('from' => '11:45', 'to' => '12:15'), 
4 => Array ('from' => '12:30', 'to' => '13:00'), 
5 => Array ('from' => '13:15', 'to' => '13:45'), 
6 => Array ('from' => '14:00', 'to' => '14:30'), 
7 => Array ('from' => '14:45', 'to' => '15:15'), 
8 => Array ('from' => '15:30', 'to' => '16:00'), 
9 => Array ('from' => '16:15', 'to' => '16:45'), 
10 => Array ('from' => '17:00', 'to' => '17:30'), 
11 => Array ('from' => '17:45', 'to' => '18:15'), 
12 => Array ('from' => '18:30', 'to' => '19:00'), 
13 => Array ('from' => '19:15', 'to' => '19:45')); 

$a2 = array(); 
foreach($array2 as $timeRange2) 
{ 
    $a2f = strtotime($timeRange2['from']); 
    $a2t = strtotime($timeRange2['to']); 
    $bInArray2 = false; 
    foreach ($array1 as $timeRange1) 
    { 
     $a1f = strtotime($timeRange1['from']); 
     $a1t = strtotime($timeRange1['to']); 

     if(($a1f >= $a2f && $a1f <= $a2t) || ($a1t >= $a2f && $a1t <= $a2t)) 
     { 
      $bInArray2 = true; 
      break; 
     } 
    } 
    if(!$bInArray2) 
     $a2[] = $timeRange2; 
} 
$array2 = $a2; 
echo var_dump($array2); 
?> 
+0

感謝您的幫助,但結果不正確。在Array1中是保留的時隙,在Array2中是可用的時隙。如您所見,每個時間段之間有15分鐘。樣本: 保留從10:00 - 10:30從09:30 - 10:00和10:15 - 10:45可用所以09:30 - 10:45的時間段不應該在結果中,導致它處於從10:00到10:30的預約時間段的範圍內。希望你現在能明白我的意思。提前致謝。 – Dirk 2014-11-01 08:55:08