For a calendar, I read start date and end date from database, and a dateRange function creates an array with one day for each key, like this:
$total_dates[]=dateRange('2012-04-01','2012-04-05');
$total_dates[]=dateRange('2012-04-06','2012-04-09');
$total_dates[]=dateRange('2012-04-10','2012-04-15');
$total_dates[]=dateRange('2012-04-17','2012-04-21');
$total_dates[]=dateRange('2012-04-24','2012-04-28');
will output:
Array (
[0] => Array ( [0] => 2012-04-01 [1] => 2012-04-02 [2] => 2012-04-03 [3] => 2012-04-04 [4] => 2012-04-05 )
[1] => Array ( [0] => 2012-04-06 [1] => 2012-04-07 [2] => 2012-04-08 [3] => 2012-04-09 )
[2] => Array ( [0] => 2012-04-10 [1] => 2012-04-11 [2] => 2012-04-12 [3] => 2012-04-13 [4] => 2012-04-14 [5] => 2012-04-15 )
[3] => Array ( [0] => 2012-04-17 [1] => 2012-04-18 [2] => 2012-04-19 [3] => 2012-04-20 [4] => 2012-04-21 )
[4] => Array ( [0] => 2012-04-24 [1] => 2012-04-25 [2] => 2012-04-26 [3] => 2012-04-27 [4] => 2012-04-28 )
)
Now it should output not-available on those dates that are included in the array and available on the free dates. There can be ONLY ONE reservation for each day (for now, later I also need to implement like morning and afternoon, so this will cross then).
The logic was to go from day1 to last day. If not, echo free, if yes, check if it was the last day of this dateRange, if yes, increase the key of the subarray...
Here is the code, but it does not increase the key if it is the last key...
reset($total_dates);
$array_i=0;
for ($i=1;$i<=$this_maxdays_month;$i++) {
if($i<10) {$i2="0".$i;} else {$i2=$i;} //if $i is 1-9, add a leading zero
if($i==1) {$month_begin=date('M',$this_date)." ";} else {$month_begin="";} //if 1st of month add e.g. Jan
if($total_dates[$array_i]) {
if (in_array("$year-month-$i2",$total_dates[$array_i])) {
echo "not available";
// now check if the last key in date range, if yes, increase key -> does not work!
if (key(array_slice($total_dates[$array_i], -1, 1, TRUE))+1==$i) {$array_i++;}
}
else {
echo "available";
}
}
}
PS: I have also tried:
$count_max_array_keys=count($total_dates[$array_i])+1;
if (!array_key_exists($count_max_array_keys,$total_dates[$array_i])) {$array_i++;}
and played with the +1 (as in the first example)...
...anyone any ideas what I am missing here??
Thank you in advance!
UPDATE: it should output (later build in a calendar-view), like:
1st April 2012: booked
2nd April 2012: booked
3rd April 2012: booked (this is the last date of this dateRange, so increase key)
4th April 2012: (not in array anymore) available
5th April 2012: available
6th April 2012: (is in the next dateRange) booked
...
hope this is clearer now, thx!
UPDATE 2: if I use if ( (count($total_dates[$array_i])-1)+1==$i) {$array_i++;}
on the exact same place as the array_slice before, following happens:
row-number $array_i output of the count-statement
1: 0 4
2: 0 4
3: 0 4
4: 0 4
5: 1 3 (this is the last key, but here $array_i should still be 0, as the increase happens after the output, and why is suddenly count only 3?)
6-30: 1 3 (it never increases again)