...

View Full Version : Help with php date constraint format



Mike123
04-01-2004, 12:06 AM
First, thanks in advance to anyone why can help me with this problem. I am a newbie and have a date constraint problem. Is the following coded correctly? I can't seem to get it to work.

case "date_range":
$dates = explode(":", $value);
$this->constraints .= "vehicle_reservations.start_date NOT BETWEEN '".$dates[0]."' AND '".$dates[1]."' AND
vehicle_reservations.end_date NOT BETWEEN '".$dates[0]."' AND '".$dates[1]."' AND ";
(vehicle_reservations.start_date IS NULL AND vehicle_reservations.end_date IS NULL) AND";
break;

Thanks,
MH

Hawkmoon
04-01-2004, 04:46 AM
Hey Mike,

What exactly are you trying to do? It looks like you want something close to the following:

case "date_range"

$arrDateRange = explode(":",$value);
$firstDate = $arrDateRange[0];
$secondDate = $arrDateRange[1];

$startDate = $this->getStartDate();
$endDate = $this->getEndDate();

if($startDate <= $firstDate ||
$startDate >= $secondDate ||
$endDate <= $firstDate ||
$endDate >= $secondDate) break;

if($startDate=="" || $endDate=="") break;

Let me know if this is what you are aiming for because there are some quicker ways to execute this.

-Hawkmoon

Mike123
04-01-2004, 05:03 AM
Thanks for replying Hawkmoon,

This is for a search feature that displays motorhomes that are available for a selected date range. I am trying to make it so it will not display motorhomes in the database that are already booked for the selected dates (double booking!). $dates[0] is the form beginning date, $dates[1] is the form ending date. vehicle_reservations.start_date are the reservations in the database.

Currently the code is allows "double booking":

$this->constraints .= "(('".$dates[0]."' < vehicle_reservations.start_date AND
'".$dates[1]."' < vehicle_reservations.start_date) OR
('".$dates[0]."' > vehicle_reservations.end_date AND
'".$dates[1]."' > vehicle_reservations.end_date) OR
(vehicle_reservations.start_date IS NULL) AND
(vehicle_reservations.end_date IS NULL)) AND";

Thanks,
Mike

Hawkmoon
04-01-2004, 05:49 AM
Gotcha...

Your AND/OR logic looks right. What values are $dates[0] and $dates[1] returning?

Mike123
04-01-2004, 06:00 AM
Maybe if you looked at the page http://www.privatemotorhomerental.com/search.php?

Just click search and all coaches will be brought up but if you put in the dates it should exclude coaches that are already booked.

Hawkmoon
04-01-2004, 06:27 AM
Make sure all your dates in the database are in yyyymmdd format and the following code would work.

-Hawk



//Start Date
$sDay = $_POST['pcf_start_date_day'];
$sMonth = $_POST['pcf_start_date_month'];
$sYear = $_POST['pcf_start_date_year'];

//End Date
$eDay = $_POST['pcf_end_date_day'];
$eMonth = $_POST['pcf_end_date_month'];
$eYear = $_POST['pcf_end_date_year'];

$sDate = $sYear . $sMonth . $sDay;
$eDate = $eYear . $eMonth . $eDay;

$sql = "SELECT * FROM ###your_database_name### ";
$sql .= " WHERE ";
$sql .= " vehicle_reservations.end_date < $sDate"; //Current Reservation ends before the queried date starts.
$sql .= " AND vehicle_reservations.start_date > $eDate"; //Current Reservation starts after the queried date ends

$result = mysql_query($sql);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum