dpka7974 2014-10-31 06:22
浏览 21

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

Hope anybody can help me here. I have 2 Arrays with given start and end times. What i need to do is to check if the time range from Array 1 is in the range of Array 2 and if it is in the range it have to be removed from Array 2.

Array 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 ) 
)

Array 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 ) 
) 

Hope there is somebody out there who can help me with this. Thanks in advance for your help!

  • 写回答

1条回答 默认 最新

  • dongxi1943 2014-10-31 07:50
    关注

    PS: Edited as the OP has provided more details.

    This should be a comment, but: o "reputation!" you made this an answer!!

    Your question was incomplete and ambiguous! (more information added Afterwards)

    Just check every available time slot against the reserved ones!, and if the available time slot is still "available", then add it to a temporary array and finally update the available time slots' array:

    <?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);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?