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

[eluser]JamesTaylor[/eluser]
I'm working with the calendar class and need some help with how to manipulate dates which i'm pulling from a DB into an array which i can pass to the calendar to display.

From my database i'm grabbing dates when a property (a house) is unavailable / booked. My example data is as follows and is currently in a variable called $booked in the controller:

Code:
Array (
[0] => Array ( [From] => 2010-10-15 [To] => 2010-10-20 )
[1] => Array ( [From] => 2010-10-25 [To] => 2010-10-30 )
)

As you can see the i'm working with dates in October for this example. I need to extract the dates in October when the property is unavailable so they can be displayed on the calendar.

What i need to end up with in accordance with the calendar class is an array which would look like the following:

Code:
Array (
[15] => 'booked',
[16] => 'booked',
[17] => 'booked',
[18] => 'booked',
[19] => 'booked',
[20] => 'booked',
[25] => 'booked',
[26] => 'booked',
[27] => 'booked',
[28] => 'booked',
[29] => 'booked',
[30] => 'booked'
)

How would i best go about doing this?

Thanks

James
#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';
}
#3

[eluser]JamesTaylor[/eluser]
Thanks for your help mddd,

I've examined this and looked through the logic to try and learn something! However when i implement it it is only giving me the two start dates in the array.

i.e the array is as follows:

Code:
Array (
[15] => booked
[25] => booked
)
#4

[eluser]mddd[/eluser]
Aha. I see I made a little mistake. See, in the part that deals with the end date, we check to see if the end date is past the current month, and if it is not, we say $enddate = $from[2]. Of course, that should have been $to[2] : we want the day of the 'to' date. Not the 'from' date because if from and to are the same day, you'll only get one day. As you have found.
#5

[eluser]JamesTaylor[/eluser]
Ah yes, i didn't pick up on that. Thanks mddd, much appreciated!




Theme © iAndrew 2016 - Forum software by © MyBB