Range help required "Resolved"

deleted

Decided to go with a mysql query instead

deleted

What I understand, is that you have two sets of dates: one range from MySQL, one range from $_POST:

This would return false:
[=== MySQL Range ===]
  [=== $_POST Range ========]

This would return true:
[=== MySQL Range =======]
  [=== $_POST Range ==]

Correct?

So basically what you have to do is compare the two sets:

if ($mysql_start_date <= $post_startdate && $mysql_end_date >= $post_end_date) {
  return true;
} else {
  return false;
}

You can easily compare Unix timestamps this way for example.

If you’re going to do it with a MySQL query, you could try the following:

// Where column 'booked' can be either 'YES' or 'NO'
$sql = "SELECT distinct(booked) FROM bookings WHERE date >= ".$post_start_date." AND date <= ".$post_end_date;
$rslt = mysql_query($sql);
$bookings = mysql_fetch_assoc($rslt);
if (in_array("YES", $bookings)) {
  // At least one day has been booked in this range
} else {
  // All days available for booking
}

Thank you for your reply.

The problem i’m having is the start and end dates are in 2 seperate table columns, I know the easiest way would be to have them in 1 columns then I could just use a mysql between function. This is why I am trying the function in the code above. I now have the following

$chkdfrom = date('Y-m-d', mktime(0,0,0,$_POST['mof'],$_POST['daf'],$_POST['yef']));
$chkdto = date('Y-m-d', mktime(0,0,0,$_POST['mo2'],$_POST['da2'],$_POST['ye2']));

function checkDateRangeArray($chkdfrom,$chkdto) { 

$reqRange=array(); 

$reqDateFrom=mktime(1,0,0,substr($chkdfrom,5,2), substr($chkdfrom,8,2),substr($chkdfrom,0,4)); 
$reqDateTo=mktime(1,0,0,substr($chkdto,5,2), substr($chkdto,8,2),substr($chkdto,0,4)); 

if ($reqDateTo>=$reqDateFrom) { 
array_push($reqRange,date('Y-m-d',$reqDateFrom)); // first entry 

while ($reqDateFrom<$reqDateTo) { 
$reqDateFrom+=86400; // add 24 hours 
array_push($reqRange,date('Y-m-d',$reqDateFrom)); 
} 
} 
return $reqRange; 
}
$required=checkDateRangeArray($chkdfrom,$chkdto);
$qryfrom = implode(',',$required);

This gives me a list of dates submitted by a user.

Is there a way I can check these dates in a database. I have tried the following

$sql=("SELECT * FROM table WHERE field = '$qryfrom' ", $connect);
$rows=mysql_num_rows($sql);

if(isset($rows))
{ 
  // do something
}
else
{
  // do the opposite
}

That didn’t work so I tried

$qryfrom = implode(' AND field = ',$required);

$sql=("SELECT * FROM table WHERE field = '$qryfrom' ", $connect);
$rows=mysql_num_rows($sql);

if(isset($rows))
{ 
  // do something
}
else
{
  // do the opposite
}

That didn’t work either, any sugestions would be much appreciated

Sponsor our Newsletter | Privacy Policy | Terms of Service