Click to See Complete Forum and Search --> : Checking dates availability using PHP / Mysql


beahawk
02-21-2007, 06:41 AM
Hello all,

I am a front end web developer, so go easy on me!

I am trying to build a system to add and check availability of dates for a car hire company.

I have built a form to add dates which are booked out and this works great. Now I am trying to build a form to query if a set period is available to hire a car or not.

I have a mysql database with the following fields:

id, day, month, year, desc

I have now built an 'availability checker' form to get the dates of the hire period which consists of the following select fields:

startday startmonth startyear
endday endmonth endyear

How can I take the values from the 'availability checker' form and query the db to see if the rental period is available?

Eventually this will query for several different cars but to keep it simple for now there is just one car for hire.

Thanks in advance for your help.

nrjCL
02-21-2007, 09:26 AM
I would just add a form with some select elements. Have the user select a month/day/year.


<select name="month">
<option value="01">January</option>
</select>

<select name="day">
<option value="05">05</option>
</select>

<select name="year">
<option value="08">2008</option>
</select>


And then submit it to a PHP script that would check the date against the database.

$date = $_POST['month'] . '-' . $_POST['day'] . '-' . $_POST['year'];

...or however the date format is stored in your DB.
Hope this helps.

beahawk
02-21-2007, 09:34 AM
Hi nrjCL,

Thanks for your response, I have created a form to collect the dates already and can submit this form ok and make the variables. What I need to do now is check these variables against the data in the db.

So I need to run a query which will tell the user if the hire period they have input is available or unavailable.

Thanks again for your help.

nrjCL
02-21-2007, 09:40 AM
Instead of checking available dates (start, and end date of a rental period) Why not have a table called "cars"
with the fields: "isRented" & "availableDate"

If the car is rented out. Set the isRented field to '1' else '0'.

If isRented == '1' set the availableDate field to the date it is available next.
Otherwise set the availableDate to 'true'.

When you query a date,
return all cars that meet this condition:
availableDate == true || $queryDate > availableDate.

nrjCL
02-21-2007, 09:53 AM
Sorry... should have read that more carefully :P

Easiest way to do this would be with PHP's date() and time()

the time() function returns the servertime in an integer value, looks something like this

$currentTime = time();
echo $currentTime;

//output is 1172072935

$nextWeek = time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs
echo $nextWeek;
//output is 1172678199


you can convert between date() and time() and when your comparing you can check if a $queryDate is between 1172072935 and 1172678199... much easier than 04/09/2007 and 04/12/2007.

Hope this helps.
www.php.net/time

beahawk
02-21-2007, 09:58 AM
The current table set up is being used by an availability calendar. The calendar pulls any dates from the db and displays them according to there availability - i.e. unavailable days are shaded out.

I was hoping to use the same table and information to check for availability when they start this 'availability checker'. Is it possible to use this same info/structure?

nrjCL
02-21-2007, 10:12 AM
Query something like this:

$checkDay = $_POST['day']
$checkMonth = $_POST['month']
$checkYear = $_POST['year']

SELECT *
FROM tablename
WHERE (
startMonth < '$checkMonth' AND
startDay < '$checkDay' AND
startYear < '$checkYear' AND

endMonth > '$checkMonth' AND
endDay > '$checkDay' AND
endYear > '$checkYear' )


If any rows are returned, the date is unavailable.

beahawk
02-21-2007, 10:12 AM
Sorry, posted there before I saw your reply.

An example entry in the database:

id - 1
day - 8
month - 03
year - 2007
desc - The car is booked out today

And the user searches for start date 6th March 2007 and end date 10th March 2007.

The period that the user would like to hire the van is unnavailable.

So using the PHP's date() and time() functions how could I query the database to tell the user the period is unavailable?

Thanks, I really appreciate your help.

beahawk
02-21-2007, 10:16 AM
Sorry - posted again before your reply.... you are too efficient!!

I will give that a try and see how it works. I will let you know how I get on.

Thanks very much!