如果已经采用时间段,则PHP检入数组

时间:2015-10-30 11:17:39

标签: php mysql arrays

如何检查范围内的某个时间段是否已经完成。

我突出了问题(图片)。

  1. 时间段:00:00 - 00:45
  2. Timeslot:03:00-6:00
  3. 时间段:05:00-10:00(这应该检查它必须大于06:00)
  4. HTML标记

     #option value: 00:00 till 23:45 with 15 minutes gaps
     <select name="start_time[]"></select>
     <select name="finish_time[]"></select>
    

    PHP:

    if ( is_array($_POST['start_time']) ) {
    
      $json_combine = array_combine($_POST['start_time'], $_POST['finish_time']);
    
      foreach ($json_combine as $jcs => $jcf ) {
        if ( ($jcs > $jcf) OR ($jcs == $jcf) ) {
           echo 'Start time must before finish time.';
           break;
        }
      }
    
    }
    

    enter image description here

2 个答案:

答案 0 :(得分:1)

也许您可以使用DateTime进行比较:

if ( is_array($_POST['start_time']) ) {
    $json_combine = array_combine($_POST['start_time'], $_POST['finish_time']);

    foreach ($json_combine as $jcs => $jcf ) {
        $d1=new DateTime($jcs);
        $d2=new DateTime($jcf);
        if ($d1 >= $d2) {
            echo 'Start time must before finish time.';
            break;
        }
    }
}

答案 1 :(得分:0)

如果我正确理解它,可以试试这个:

foreach ($_POST['start_time'] as $index => $key) {

    $start_time = strtotime($_POST['start_time'][$index+1]);
    $finish_time = strtotime($_POST['finish_time'][$index]);

    if ( ($start_time > $finish_time) OR ($start_time == $finish_time) ) {
       echo 'Start time must before finish time.';
       break;
  }

}