Basic PHP grid calendars



Here’s how to make a basic PHP grid calendar. I started with this code and stripped it down to bare bones.
http://www.phpjabbers.com/how-to-make-a-php-calendar-php26.html

Here’s a simple function that takes a month and year and it will mark today’s grid square on the calendar with the class “cal-today”

function grid_cal($m,$y){
	$tabs = "\n\t\t\t\t";
	$w_days = array("S","M","T","W","T","F","S");
	$stamp = mktime(0,0,0,$m,1,$y);
	$maxday = date("t",$stamp);
	$thismonth = getdate ($stamp);
	$startday = $thismonth['wday'];			
?>

				<table class="cal-table">
					<tr class="cal-month">
						<td colspan="7"><?php echo date("F",$stamp).' '. date("Y",$stamp); ?></td>
					</tr>
					<tr class="cal-days">
						<?php foreach($w_days as $d) { echo "$tabs\t\t<td>$d</td>"; } ?>
				
					</tr>
<?php
	$curr_month = (date("F Y")==date("F Y",$stamp))? TRUE : FALSE; // if this calendar is displaying current month
	$today = date("j"); 
	for ($i=0; $i<($maxday+$startday); $i++) {
		if(($i % 7) == 0 ) echo "$tabs\t<tr class='cal-date'>";
		$date_class = ($curr_month && $i - $startday + 1 == $today)? " class='cal-today'": "";
		if($i < $startday) echo "$tabs\t\t<td></td>";
		else echo "$tabs\t\t<td{$date_class}>". ($i - $startday + 1) . "</td>";
		if(($i % 7) == 6 ) echo "$tabs\t</tr>\n";
	}
echo "$tabs</table>";

}

usage…

<?php grid_cal(date("n"),date("Y")); ?>


Posted

in

by

Tags:

Comments

Leave a Reply