Solved

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

Posted on 2004-10-31
201 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
Question by:thaistixxx
    4 Comments
     
    LVL 12

    Expert Comment

    by:minichicken
    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
    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
    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:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now