比较数据库中从表单到日期/时间的日期/时间

时间:2014-01-28 13:37:58

标签: php mysql

更新

我编辑了我的代码,所以现在正确检查了时间:

while($row = mysqli_fetch_array($result)){
  $rid = $row['roomid'];
  $begin = $row['start'];
  $bval = strtotime($begin);
  $einde = $row['end'];
  $eval = strtotime($einde);

  $staco = strtotime($start); //starttime posted from form
  $endco = strtotime($end);  //stoptime posted from form


  $abegin = array(sta => $bval);
  $aeinde = array(sto => $eval);
  // print_r($abegin);
  // print_r($aeinde);
  foreach($aeinde as $sto) {
  if($staco <= $sto) {$checksto = 1;}
  else {$checksto = 0;}
  }

  foreach($abegin as $sta) {
  if($endco <= $sta) {$checksta = 1;}
  else {$checksta = 0;}
  }


  if($checksta == $checksto) {$ok = 1;} else {$ok = 0;}  

  print_r($ok);

  }
  }
}

现在:我如何检查$ ok是否包含一个或多个0(不预订房间)或全部1(预订房间)。

$roomstate = array(state => $ok)会产生多个数组:

Array ( [state] => 0 ) Array ( [state] => 1 ) Array ( [state] => 1 )

我做错了,因为我认为应该可以在一个数组中获取所有不同的$ok然后

if(in_array(0,$roomstate)) { echo "Do not book";} else {$bookitsql = "INSERT INTO reservations ...";}
UPDATE: There is a flaw in my original logic to check availabilty with the rooms that needs to be solved first: now the rooms are not checked correct, so it is impossible to answer this question since the correct data is not displayed. My apologies.

For a system that books rooms I need to check with a new booking if the room is already is booked at the moment. The booking works with a form, and then it compares the results from the form with the content in the database for that room on that date.

    while($row = mysqli_fetch_array($result)){
      $rid = $row['roomid'];
      $begin = $row['start'];
      $bval = strtotime($begin);
      $staco = strtotime($start);
      $einde = $row['end'];
      $eval = strtotime($einde);
      $endco = strtotime($end);

      $abegin = array(sta => $bval);
      $aeinde = array(sto => $eval);

        foreach($abegin as $sta) {
        if($staco  $checksta,checkstop => $checksto);
          print_r($meh);
    }

BetterQuestion:

Now I get `$staco` and `$endco`, which are the start and stoptime from the form.
I also get `$sta` and `$sto`, which are multiple start and stoptimes from the database.

Example: 
existing reservations:

        sta     sto
    1:  0800    0959
    2:  1130    1259

So now when I get `$staco = 1000` and `$endco = 1114` it doesn't check right.
It only works if the new reservation is later than all the other reservations in the database. How can I solve this?

2 个答案:

答案 0 :(得分:0)

您的目标伪代码显示您希望它全部为0. $ meh是一个数组。 所以你现在所做的就是在$ meh上使用foreach,然后在出现1时将其标记为Occupied。如果没有发生这种情况,你可以假设$ meh是免费的,你做预订。

$roomState = 0;
foreach($meh as $mehValue){
  if($mehValue == 1){
    $roomState = 1;
  }
}
if($roomState == 1){
  print_r("room is occupied");
} else {
  //insert stuff
}

答案 1 :(得分:0)

通过在我的sql语句中检查日期来采用不同的方法。 因此,表单发布$ start和$ end(当用户想要预订时)。 现在我的代码看起来像这样(我认为看起来没问题):

//form and other errormessages like username==0; $start>$end etcetera
else {
  $reservationsql = "SELECT * FROM reservations WHERE roomid = '$roomid' AND ('$start' between start AND end) OR ('$end' between start AND end) OR ('$start' >= start AND '$end' <= end) OR ('$start' <= start AND '$end' >= end)";
  $result = mysqli_query($link,$reservationsql) or die(mysql_error());
  $num = mysqli_num_rows($result);

  if($num>0) {"Error: room already booked at that moment";}
  else {//$query = "INSERT INTO ..."}
}
}mysqli_close();

感谢您在此提供的所有帮助,并与我一起思考。