Welcome Guest, Not a member yet? Register   Sign In
Extracting / Manipulating data from one array to generate another
#2

[eluser]mddd[/eluser]
I'm guessing the dates in $booked can cross monthly boundaries. So that $booked[0] could also be : [from] => 2010-09-20 [to] => 2010-10-10 ?
In that case I would do something like this:
Code:
// empty array to store the results
$output = array();
// set variables to know which period we're supposed to be in
$currentyear = 2010;
$currentmonth = 10;

// take every period and get the booked days from it
foreach ($booked as $period)
{
   // make year, month and day accessible
   $from = explode('-', $period['from']);
   $to = explode('-', $period['to']);

   // check if the startdate is before the current month. if so, set start day to 1; otherwise to the start of the period
   if ($from[0]<$currentyear || $from[1]<$currentmonth)
      $startday = 1;
   else
      $startday = $from[2];

   // same thing for the end. if it's past the current month set end day to last day of current month
   if ($to[0] > $currentyear || $to[1]>$currentmonth)
      $endday = date('t', mktime(0,0,0,$currentmonth,1,$currentyear));
   else
      $endday = $to[2]; // --- by mistake, this said $endday = $from[2]; -- see posts below.

   // now mark the days booked
   for ($i = $startday; $i<=$endday; $i++)
      $output[$i] = 'booked';
}


Messages In This Thread
Extracting / Manipulating data from one array to generate another - by El Forum - 07-27-2010, 05:12 AM



Theme © iAndrew 2016 - Forum software by © MyBB