[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-31
4
Medium Priority
?
270 Views
Last Modified: 2008-03-06
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";
}



0
Comment
Question by:thaistixxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:minichicken
ID: 12457874
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
 
LVL 4

Expert Comment

by:Skonen
ID: 12457895
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
 
LVL 4

Expert Comment

by:Skonen
ID: 12457906
minichicken: strtotime isn't really required for the format he is using, because it is in a form of (year > month > day).
0
 
LVL 12

Accepted Solution

by:
minichicken earned 200 total points
ID: 12457942
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question