How to check if date is between $a and $b

I'm trying to create a reservation system and I want to prevent the user from reserving the same dates.

I've got two variables set by the user's current selection:
$arrive = "2004/6/1";
$depart = "2004/6/5";

And two variables that come from the array created by their previous selections:
$chk_arrive = "2004/6/2";
$chk_depart = "2004/6/4";

I need a script to check if the current selection is already reserved in the array. In the example variables above, you see that the current selection conflicts with the previous selection.

Here is what I have so far, but doesn't seem to work too well.

if (($chk_arrive >= $arrive) && ($chk_arrive <= $depart) || ($chk_depart >= $arrive) && ($chk_depart <= $depart)) {
  $already_reserved = "yes";
}



thaistixxxAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
minichickenConnect With a Mentor Commented:
Hmm, that true, but i think it good to use it whenever you are comparing time....
If that's not the problem then i think it the if condition that is the problem.....
I added a few brackets to it as OR goes before AND in an if statement.

****************************************************
<?
$arrive = strtotime("2004/6/1");
$depart = strtotime("2004/6/5");

$chk_arrive = strtotime("2004/6/2");
$chk_depart = strtotime("2004/6/4");

if ((($chk_arrive >= $arrive) && ($chk_arrive <= $depart)) || (($chk_depart >= $arrive) && ($chk_depart <= $depart)))
{
  $already_reserved = "yes";
}
?>
0
 
minichickenCommented:
Hi

Use the strtotime(); function:

*****************************************************

$arrive = strtotime("2004/6/1");
$depart = strtotime("2004/6/5");

$chk_arrive = strtotime("2004/6/2");
$chk_depart = strtotime("2004/6/4");

if (($chk_arrive >= $arrive) && ($chk_arrive <= $depart) || ($chk_depart >= $arrive) && ($chk_depart <= $depart)) {
  $already_reserved = "yes";
}
0
 
SkonenCommented:
if ($chk_arrive >= $arrive && $chk_arrive <= $depart && $chk_depart >= $arrive && $chk_depart <= $depart) {
   $already_reserved = "yes";
}

I'm not entirely sure what you're trying to do, your question was somewhat confusing. But I think the above is what you're looking for.
0
 
SkonenCommented:
minichicken: strtotime isn't really required for the format he is using, because it is in a form of (year > month > day).
0
All Courses

From novice to tech pro — start learning today.