Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question VIA PHP: Calculating the Duration of User Inputted Dates

    Hi Guys

    I have created a flight/hotel itinerary and I need to work out the duration of the customers' holiday in order to work out how much it will cost to charge that person per night at their selected hotel.

    The problem I have is that the dates are going to vary due to user inputs in which I have used jquery's "datepicker"

    If you need me to post any code I will be more than happy to post it

    Cheers

    George

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    if your using the datepicker then you should be able to impose standardize date formats.

    can you post the date format that your using?

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    PHP Code:
                $(document).ready(function() {
                $(
    "#datepicker").datepicker({ 
                    
    dateFormat'yy/mm/dd'
                    
    showAnim'puff',
                    
    showOn'both',
                    
    buttonImage'css/images/calendar-icon.gif',
                    
    buttonImageOnlytrue
                    
    });
                  });
      
                 $(
    document).ready(function() {
                $(
    "#returndatepicker").datepicker({ 
                    
    dateFormat'yy/mm/dd'
                    
    showAnim'puff',
                    
    showOn'both',
                    
    buttonImage'css/images/calendar-icon.gif',
                    
    buttonImageOnlytrue 
                    
    });
                  }); 

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, so you will need to reformat your date fields after the submission.

    you can get the number of days selected like this;

    PHP Code:
    $days = (strtotime(date("Y-m-d")) - strtotime("2011-05-15")) / (60 60 24);
    print 
    $days

  • #5
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    thanks very much angst
    I am not sure
    I figured a way out but in my duration box when I input my dates it is only showing '0'.

    Not sure if you think you could see any possible problems?

    PHP Code:
    <?php

        date_default_timezone_set
    ('Europe/London');
        
        
    $doc = new DOMDocument('1.0''utf-8');
        
    $doc->formatOutput true;
        
        
    $out = new DateTime($doc->getElementById("datepicker"));
        
    $in = new DateTime($doc->getElementById("returndatepicker"));
        
    $duration $out->diff($in)->format('%a');
         
        
    ?>
        
        <table style="border: 5px solid black;">
            <tr style="border: 1px solid red;">
                <td style="border: 4px solid green; text-align: center;"><u>Check In</u></td>
                <td style="border: 4px solid green; text-align: center;"><u>Check Out</u></td>
            </tr>
            <tr style="border: 1px solid red;">
                <td style="border: 3px solid blue;"><input type="text" id="datepicker" name="checkInDate"/></td>
                <td style="border: 3px solid blue;"><input type="text" id="returndatepicker" name="checkOutDate"/></td>
            </tr>
        </table><br />
        <table>
            <tr>
                <td style="border: 5px solid red;">Duration = <?php print $duration?></td>
            </tr>
        </table>

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    not sure that $doc->getElementById("datepicker") works the way your using it. so my understanding ( which is limited for DOM ), you can't use it to gather POST/GET date from a form. so where is your data coming from?

    also I assume that $out->diff is the day diff code I providing above?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $out->diff is a 5.3+ functionality of the DateTime class. Given two DateTime's, it will return a DateInterval.

    Angst is correct, you are not giving it any data to work with. The dom is useless in this example, you cannot detect client data by scanning the dom. You need to check the input of the submitted form, which appears to be under checkInDate and checkOutDate.

  • #8
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Oh really thats my naivety towards php then i guess, apologies!
    this was just a practice file I created, in my actually work I have a numerous files...
    1. hotels.php
    in which a user will input check out date, check in date, resort, hotel star min/max and room type.

    2. hotels_results.php
    this connects to the database I have been given which then will provide the results a user inputted.

    3. itinerary_hotels.php
    and finally i send the users chosen hotels to this file and the necessary details about their chosen hotel are shown in this page.

    i saw the only possible way of getting the users input from my 2 jquery datepickers was to use getElementById to take their id's if you see what I was attempting?

    What would you recommend for this situation?

    Thanks again

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by georgesofroniou View Post
    Oh really thats my naivety towards php then i guess, apologies!
    this was just a practice file I created, in my actually work I have a numerous files...
    1. hotels.php
    in which a user will input check out date, check in date, resort, hotel star min/max and room type.

    2. hotels_results.php
    this connects to the database I have been given which then will provide the results a user inputted.

    3. itinerary_hotels.php
    and finally i send the users chosen hotels to this file and the necessary details about their chosen hotel are shown in this page.

    i saw the only possible way of getting the users input from my 2 jquery datepickers was to use getElementById to take their id's if you see what I was attempting?

    What would you recommend for this situation?

    Thanks again
    PHP is server side, the only thing it can understand is what you provide it. So the data provided by the JQuery must be provided in a way that PHP can understand, namely a form input. I don't know JQuery so I don't know what you need to modify to populate a form field. Once its been populated, PHP can retrieve this from the $_GET/$_POST superglobal in the same offset provided by the name attribute of the input field. I'd assume it will be formatted in a D/M/Y as you have in the JQuery, but PHP can convert that to an integer using strtotime (or just use the DateTime as you already have).

  • #10
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Fou Lou,

    as soon as I sent the reply to your earlier message I had a thought in hotels_results.php Ive declared a variable and then used $_REQUEST to get the value inputted into the datepickers....

    PHP Code:
    <?php
                    
                    
    include("database.php");
                    
                    
    $flight_tab="flight";
                    
    $hotel_tab="hotel";
                    
                    
    $cInDate=$_REQUEST["checkInDate"];
                    
    $cOutDate=$_REQUEST["checkOutDate"];
                    
    $resort=$_REQUEST["hResortLoc"];
                    
    $minHot=$_REQUEST["starMinHotel"];
                    
    $maxHot=$_REQUEST["starMaxHotel"];
    ..........
    as you can see i have $cInDate and $cOutDate could I do this code instead....

    PHP Code:
    <?php

        date_default_timezone_set
    ('Europe/London');
        
        
    $out = new DateTime($cInDate);
        
    $in = new DateTime($cOutDate);
        
    $duration $out->diff($in)->format('%a');
         
        
    ?>

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    thats correct, of course with the data being submitted via a <form> post.

    also @Fou-Lu
    $out->diff is a 5.3+ functionality of the DateTime class. Given two DateTime's, it will return a DateInterval.
    had no idea, but good to know!
    Last edited by angst; 05-24-2011 at 09:29 PM.

  • #12
    New Coder
    Join Date
    Nov 2010
    Location
    England
    Posts
    23
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Ah brilliant! sorry bit of a newbie to this coding

    Yes all this work has been contained in <form> so I can send the data to the necessary pages.

    appreciate the help from you guys fingers crossed that this works now!!


    **UPDATE**

    Unfortunately I could not get it to work
    for now I am just going to set up a simple drop down box allowing the user to choose how many nights they wish to stay
    Last edited by georgesofroniou; 05-24-2011 at 09:39 PM. Reason: code reasons


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •