Jump to content

date problem


pouncer

Recommended Posts

say in my table (for hotel bookings) i have this:

 

start data end_date (fields)

10-nov-07 15-nov-07

19-nov-07 25-nov-07

 

how do i return the days where no bookings are taking place? i.e:

 

16,17,18 and after 25th november

 

i just want to somehow get the available days

 

Link to comment
Share on other sites

For starters, I'd recommend you change the data type in which you store your dates to a DATE type. This will allow you to very easily query against it. DATE types are stored as YYYY-MM-DD and can be used in MySQL calculations as well as easily parsed into PHP using functions such as strtotime(). So, assuming you have a DATE type on your database, something like this would work well. This is coming from the perspective of a calendar script, so you could modify it however you like.

<?php
$year = 2007;
$month = 11; // November or date('n') for current month
$days_in_month = date('t', mktime(0,0,0,$month,1,$year));
$available_days = array();
for ($i = 1; $i <= $days_in_month; $i++) // loop through all the days of the month
{
  $ts = mktime(0,0,0,$month,$i,$year); // timestamp for current day in loop
  $date = date('Y-m-d', $ts); // queryable format
  $sql = mysql_query("SELECT * FROM my_table WHERE '$date' BETWEEN start_date AND end_date");
  if (mysql_num_rows($sql) == 0) // no results, so add it to availabilities
  {
    $available_days[] = $i;
  }
}

echo "Days available: " . implode(', ', $available_days);
?>

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.