PHP将2个阵列与时间范围进行比较

时间:2014-10-31 06:22:29

标签: php arrays time compare range

希望有人能在这里帮助我。 我有两个具有给定开始和结束时间的数组。 我需要做的是检查数组1的时间范围是否在数组2的范围内,如果它在范围内,则必须从数组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 ) 
) 

希望有人可以帮我解决这个问题。 在此先感谢您的帮助!

1 个答案:

答案 0 :(得分: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);
?>