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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
minichickenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.