Many people have asked how to create their own event calendar. If you look around, there are several ways to do it and even more people willing to sell you their ideas of how it should be done.

I am going to show you how to create a basic calendar and how to save events in a MySQL database. We will be using PHP and MySQL commands. So if you don't know them or need a refresher, check out the tutorials at http://www.w3schools.com/.


We'll start by creating our calendar. For those of you who just want the code, here it is:

<?
// Get the month and year reference in the link, if they are provided
$mth = $_GET['mth'];
$yr = $_GET['yr'];

if(isset($mth) and $mth > 0){
$month=$mth;}else{
$month= date("m");}

if (!isset($yr)) {$year = date("Y");} else { $year = $yr;} //If year variable is not set, make it this year

$day= date("d"); // Today's date

// Calculate Next Month and Advance Year if needed
$nextm = $month + 1;
$nextyr = $year;
if ($nextm >= 13) {$nextm = 1; $nextyr = $year + 1;}

// Calculate Previous Month and Subtract Year if needed
$prevm = $month - 1;
$prevyr = $year;
if ($prevm <= 0) {$prevm = 12; $prevyr = $year - 1;}

// Calculate and set variables for calendar display
$no_of_days = date('t',mktime(0,0,0,$month,1,$year)); // This is to calculate number of days in a month

$mn=date('M',mktime(0,0,0,$month,1,$year)); // Month is calculated to display at the top of the calendar

$yn=date('Y',mktime(0,0,0,$month,1,$year)); // Year is calculated to display at the top of the calendar

$j= date('w',mktime(0,0,0,$month,1,$year)); // This will calculate the week day of the first day of the month

for($k=1; $k<=$j; $k++){ // Adjustment of date starting
$adj .="<td> </td>";
}

?>

<html>
<head>
<title>Calendar Demo</title>
</head>
<body>
<?
/// Starting of top line showing name of the days of the week
?>
<table border='1' bordercolor='#920201' cellspacing='0' cellpadding='0' align=center width="840">
<tr>
<td>
<table cellspacing='0' cellpadding='0' align=center width='100%' border='1'>
<td align=center ><font size='3' face='Tahoma'> <a href='calendar.php?mth=<?=$prevm?>&yr=<?=$prevyr?>'><</a></font> </td>
<td colspan='5' align=center ><font size='3' face='Tahoma'><?=$mn?> <?=$yn?></font> </td>
<td align=center ><font size='3' face='Tahoma'> <a href='calendar.php?mth=<?=$nextm?>&yr=<?=$nextyr?>'>></a></font> </td>
</tr>
<tr>
<td width="120"><font size='3' face='Tahoma'><b>Sun</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Mon</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Tue</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Wed</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Thu</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Fri</b></font></td>
<td width="120"><font size='3' face='Tahoma'><b>Sat</b></font></td>
</tr>
<tr>
<?

////// End of the top line showing name of the days of the week//////////

//////// Starting of the days//////////
for($i=1;$i<=$no_of_days;$i++){
echo $adj."<td valign=top><font size='2' face='Tahoma'>$i<br><br>"; // This will display the date inside the calendar cell
echo " </font>";
echo "<p> </p>";
echo "</td>";
$adj='';
$j ++;
if($j==7){echo "</tr><tr>";
$j=0;}

}
?>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>