...

View Full Version : VIA PHP: Calculating the Duration of User Inputted Dates



georgesofroniou
05-24-2011, 12:35 PM
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

angst
05-24-2011, 02:50 PM
if your using the datepicker then you should be able to impose standardize date formats.

can you post the date format that your using?

georgesofroniou
05-24-2011, 06:01 PM
$(document).ready(function() {
$("#datepicker").datepicker({
dateFormat: 'yy/mm/dd',
showAnim: 'puff',
showOn: 'both',
buttonImage: 'css/images/calendar-icon.gif',
buttonImageOnly: true
});
});

$(document).ready(function() {
$("#returndatepicker").datepicker({
dateFormat: 'yy/mm/dd',
showAnim: 'puff',
showOn: 'both',
buttonImage: 'css/images/calendar-icon.gif',
buttonImageOnly: true
});
});

angst
05-24-2011, 06:09 PM
ok, so you will need to reformat your date fields after the submission.

you can get the number of days selected like this;



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

georgesofroniou
05-24-2011, 06:50 PM
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

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>

angst
05-24-2011, 06:55 PM
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?

Fou-Lu
05-24-2011, 07:57 PM
$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.

georgesofroniou
05-24-2011, 09:01 PM
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

Fou-Lu
05-24-2011, 09:10 PM
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).

georgesofroniou
05-24-2011, 09:15 PM
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

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

date_default_timezone_set('Europe/London');

$out = new DateTime($cInDate);
$in = new DateTime($cOutDate);
$duration = $out->diff($in)->format('%a');

?>

angst
05-24-2011, 09:20 PM
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!

georgesofroniou
05-24-2011, 09:29 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum