A calendar in less than 50 lines of php

It ain’t pretty by a long stretch, but the fact that it can fit into less than 50 lines of code and could be easily converted to a function for use in existing projects makes me a very happy camper, hopefully it will make it into the TheBushwickDream in the near future.

<?php
$month = $_GET['month'];
$days =  cal_days_in_month(CAL_GREGORIAN, $month,'2011'); // num of days in month

$ts = mktime(0,0,1,$month,1,2011);
$month_name = date('F',$ts);
$first_day = date('w',$ts); // numeric for the first day in the month

// 0 = Sunday, 6 = Saturday, layout is Sunday to Saturday

$d = 1;
print "<h3>$month_name</h3>";
print "first day is $first_day, $days in month!<br />";
print "Su, Mo, Tu, Wd, Th, Fr, Sa<br />";
print '<table width="50%" height="50%" cellpadding="50%" border="1">
<tr>
<td>Sunday</td>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
<td>Saturday</td>
</tr>';

for($week = 1; $week <= 5; $week++){
	print '<tr>';
	if($week == 1){
		for($day = 0; $day <= 6; $day++){
			if($day >= $first_day){
				print '<td>'.$d.'</td>';
				$d++;
			} else {
				print "<td> </td>";
			}
		}
	} else {
		for($day = 0; $day <= 6; $day++){
			if($d <= $days){
				print '<td>'.$d.'</td>';
				$d++;
			} else {
				print "<td> </td>";
			}
		}
	} print '</tr>';
}
print '</table>';
?>

Leave a Comment

Close Bitnami banner
Bitnami