Link to home
Start Free TrialLog in
Avatar of jbpeake
jbpeake

asked on

Display 12 months instead of 1 month in a PHP calendar?

In the calendar below I would like to display 12 months on a page instead of just one, maybe three months wide and four months tall and the ability to link from one year to the next or the previous.  Any direction appreciated.
<?php
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
// Determine Next Month
$any_date = "2008-06-04";
echo date('Y-m-d',strtotime("$date + 1 months"));
$nextmonth = date('m', $date);
 
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
 
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year &nbsp;&nbsp; $nextmonth</th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
} 
 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
 
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
 
echo "</tr></table>"; 
 
?>

Open in new window

Avatar of Roger Baklund
Roger Baklund
Flag of Norway image

Try this:

function show_month($date) { 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
// Determine Next Month
 
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
 
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
} 
 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
 
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
 
echo "</tr></table>"; 
}
?>
<table><tr>
<td><?php show_month(mktime(0,0,0,1)); ?></td>
<td><?php show_month(mktime(0,0,0,2)); ?></td>
<td><?php show_month(mktime(0,0,0,3)); ?></td>
</tr>
<tr>
<td><?php show_month(mktime(0,0,0,4)); ?></td>
<td><?php show_month(mktime(0,0,0,5)); ?></td>
<td><?php show_month(mktime(0,0,0,6)); ?></td>
</tr>
<tr>
<td><?php show_month(mktime(0,0,0,7)); ?></td>
<td><?php show_month(mktime(0,0,0,8)); ?></td>
<td><?php show_month(mktime(0,0,0,9)); ?></td>
</tr>
<tr>
<td><?php show_month(mktime(0,0,0,10)); ?></td>
<td><?php show_month(mktime(0,0,0,11)); ?></td>
<td><?php show_month(mktime(0,0,0,12)); ?></td>
</tr>
</table>

Open in new window

I forgot the <?php tag at the start. You may want to style the table, for instance:

table td {vertical-align:top;}
table td table td {text-align:center;}

You are probably goint to use this on a page which might have other tables, so you should put classes on the table elements. Change line 76:

<table class="yearCalendar">

...and the css:

table.yearCalendar td {vertical-align:top;}
table.yearCalendar td table td {text-align:center;}
ASKER CERTIFIED SOLUTION
Avatar of Siridivi
Siridivi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial