[thelist] date ranges

Matt Warden mwarden at gmail.com
Wed Jul 6 16:49:20 CDT 2005


Mike,

On 7/6/05, Mike D. Johnson <mdjohnson at bcbe.org> wrote:
> I have items that are reserved for certain dates.
> 
> In my database, I have reservationStartDate and reservationEndDate.  How
> do I get an array with all of the days an item is unavailable?
> 
> I need one array with all start dates, all end dates and all days in
> between those two.
> 
> I'm using ASP and an Access DB. The start and end dates in the DB are
> Date/Time values.
> 
> The final goal is to display a table-style calendar with the available
> dates indicated.

I was developing something like this a couple employments ago. I ended
up creating a custom data structure, because I had to be able to
modify the schedule easily and of course an array would require me to
do expensive shifting and such.

Since you are only reading this information, you should be fine with
an array. However, you do not need to include all days within each
range. This would be quite a waste of space. For each day, you would
simply have logic to show something as reserved if the day is within
the range for that something. Somethign along the lines (off the top
of my head, so grain of salt and whatnot):

$dayreservations = Array();
loop over days
{
   // loop over all reservations (probably for 
   // this month or some such restriction)
   foreach ($reservations as $reservation)
   {
      // if current day in range
      if ($day >= $reservation['begindate'] 
            || $day < $reservation['enddate'])
      {
         // append reference to this array to this day
         $dayreservations[$day][] =& $reservation;
      } // end if
   } // end foreach
} // end loop over days

-- 
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list