duanjiong1952 2011-01-19 06:36
浏览 46

查找传递的时间是否在php数组的范围内

$MyArr = array(
 array('1', 'Morning', '09:00:00','10:30:00'),
 array('2', 'Late Morning', '10:31:00','12:00:00'),
 array('3', 'Lunch', '12:01:00','14:00:00'),
 array('4', 'Afternoon', '14:01:00','16:00:00'),
 array('5', 'Evening', '16:01:00','19:00:00'),
);

I would like to create a function to check something like this

if '14:00:00', '16:00:00' is passed and can be found in that array it should return the first 2 values i.e. 4 and Afternoon

Here's the Test scenario:

'09:10:00','10:30:00' -> pass (return '1', 'Morning' )

'12:00:00','14:30:00' -> Fail

'10:30:00','12:00:00' -> should also pass (yeah, i need to add 1 minute to start time if it doesnt fit) (return '2', ' Late Morning' )

'09:00:00','16:00:00' -> Fail

How can i write code for this?

  • 写回答

1条回答 默认 最新

  • dongrong7883 2011-01-19 07:09
    关注

    I Think this is what you meant. I did a couple of different versions, but you can try them locally and see what they do (or take a look at the ideone working example)

      <?php
        header('Content-Type: text/plain');
    
        $MyArr = array(
          array('1', 'Morning', '09:00:00','10:30:00'),
          array('2', 'Late Morning', '10:31:00','12:00:00'),
          array('3', 'Lunch', '12:01:00','14:00:00'),
          array('4', 'Afternoon', '14:01:00','16:00:00'),
          array('5', 'Evening', '16:01:00','19:00:00'),
        );
    
        // first one, find array entries that fall between the time specified
        function in_date_range($array, $start = '00:00:00',$end = '23:59:59')
        {
          $start = strtotime(date('Y-m-d '.$start));
          $end = strtotime(date('Y-m-d '.$end));
    
          $results = Array();
          foreach ($array as $arr)
          {
            // check if $arr times fall within $start/$end
            //if (strtotime(date('Y-m-d '.$arr[2])) >= $start && strtotime(date('Y-m-d '.$arr[3])) <= $end)
            // check if $start/end fall within $arr times
            if ($start >= strtotime(date('Y-m-d '.$arr[2])) && $end <= strtotime(date('Y-m-d '.$arr[3])))
              $results[] = Array($arr[0],$arr[1]);
          }
          return $results;
        }
    
        // secon one, find array entries that surround the time supplied
        function in_date_range2($array, $time = '12:00:00')
        {
          $time = strtotime(date('Y-m-d '.$time));
    
          $results = Array();
          foreach ($array as $arr)
          {
            if ($time >= strtotime(date('Y-m-d '.$arr[2])) && $time <= strtotime(date('Y-m-d '.$arr[3])))
              $results[] = Array($arr[0],$arr[1]);
          }
          return $results;
        }
    
        print_r(in_date_range($MyArr,'14:00:00','16:00:00'));
        print_r(in_date_range($MyArr,'09:00:00','12:00:00'));
        echo "
    
    ";
        print_r(in_date_range2($MyArr,'14:00:00'));
        print_r(in_date_range2($MyArr,'16:00:00'));
      ?>
    

    EDIT
    updated: See the comments in the in_date_range function (regarding how it looks at the data).

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100