some calendar logic help
Posted: Fri Mar 21, 2008 9:29 pm
So I'm making a rolling calendar.. it will display the current month plus the next two months. What I have right now is working (since it is march), but if the current month is november or december, it won't display into the following year because of how i'm doing my for() loop.
Here's the complete script
And here's the part that's giving me problems:
$currentMonth + 3 will go over 12.
Here's the complete script
Code: Select all
<?php
//array of months
$months = array(
'January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
'September' => 9,
'October' => 10,
'November' => 11,
'December' => 12
);
$monthsF = array_flip($months);
//is this a leap year?
$leapYear = ((date('Y') % 4) == 0);
//current month and year
$currentMonth = date('n');
$currentYear = date('Y');
//number of days in each month
$monthDays = array(
1 => range(1, 31),
2 => $leapYear ? range (1, 29) : range(1, 28),
3 => range(1, 31),
4 => range(1, 30),
5 => range(1, 31),
6 => range(1, 30),
7 => range(1, 31),
8 => range(1, 31),
9 => range(1, 30),
10 => range(1, 31),
11 => range(1, 30),
12 => range(1, 31)
);
for ($month = $currentMonth; $month < $currentMonth + 3 ; $month++)
{
//what day does the first of the month fall on?
$startDayOfWeek = date('w', mktime(0, 0, 0, $month, 1, $currentYear));
//start the table
echo '<table cellspacing="0" cellpadding="2" border="0" style="border: solid 1px #000;">
<tr>
<td colspan="7" align="center" style="border-bottom: solid 1px #000;">' . $monthsF[$month] . '</td>
</tr>
<tr>
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>';
//show blank table cells till first day of month occurs
for ($i = 0; $i < $startDayOfWeek; $i++)
{
echo '<td> </td>';
}
//start the iteration at the number of days till the first day of the month occurs
$it = $startDayOfWeek;
//loop through the month's worth of days
foreach ($monthDays[$month] AS $day)
{
if ($it == 7)
{
echo '</tr><tr>';
$it = 0;
}
echo '<td align="center">' . $day . '</td>';
$it++;
}
//end the table
echo '</table><br />';
}Code: Select all
for ($month = $currentMonth; $month < $currentMonth + 3 ; $month++)