This tutorial demonstrates how to build a dynamic monthly event planner and form generator using PHP. By combining PHP's native DateTimeImmutable object with loops and HTML table structures, you can generate a fully interactive calendar grid where users can input custom events for any day of the month.
date('w').You can copy the complete PHP script below and use it on your local server:
<?php
session_start();
$prm = filter_input(INPUT_GET, 'prm', FILTER_VALIDATE_INT);
$chm = filter_input(INPUT_GET, 'chm', FILTER_VALIDATE_INT);
$current_month = $prm ? $prm : (int)date("n");
$current_year = (int)date("Y");
try {
$date_obj = DateTimeImmutable::createFromFormat('!Y-n-j', "$current_year-$current_month-1");
if ($chm !== null && $chm !== 0) {
$date_obj = $date_obj->modify(($chm > 0 ? "+{$chm} month" : "{$chm} month"));
}
} catch (Exception $e) {
$date_obj = new DateTimeImmutable('first day of this month');
}
$m = (int)$date_obj->format('n');
$y = (int)$date_obj->format('Y');
$no_of_days = (int)$date_obj->format('t');
$mn = $date_obj->format('M');
$yn = $date_obj->format('Y');
$j = (int)$date_obj->format('w');
$adj = '';
for($k = 1; $k <= $j; $k++){
$adj .="<td> </td>";
}
$body = "<form method='post' action='php_monthly_plannerck.php' name='f1'><input type='hidden' name='prm' value='$m'>";
$body .= "<table border='1' bordercolor='#FFFF00' cellspacing='0' cellpadding='0' align='center' width='90%'><tr><td>";
$body .= "<table cellspacing='0' cellpadding='0' align='center' width='100%' border='1'>
<tr>
<td align='center' bgcolor='#ffff00'><a href='php_monthly_planner.php?prm=$m&chm=-1' rel='nofollow'><</a></td>
<td colspan='5' align='center' bgcolor='#ffff00'><b>$mn $yn</b></td>
<td align='center' bgcolor='#ffff00'><a href='php_monthly_planner.php?prm=$m&chm=1' rel='nofollow'>></a></td>
</tr>
<tr>";
$body .= "<td><b>Sun</b></td><td><b>Mon</b></td><td><b>Tue</b></td><td><b>Wed</b></td><td><b>Thu</b></td><td><b>Fri</b></td><td><b>Sat</b></td></tr><tr>";
for($i = 1; $i <= $no_of_days; $i++){
$body .= $adj . "<td valign='top'>$i<br><textarea name='t[$i]' rows='2' cols='7'></textarea></td>";
$adj = '';
$j++;
if($j == 7){
$body .= "</tr><tr>";
$j = 0;
}
}
$body .= "</tr><tr><td colspan='7' align='center'><b>plus2net.com Calendar Generator</b></td></tr>";
$body .= "</table></td></tr></table>";
$body .= "<table border='0' width='50%' cellspacing='0' cellpadding='0' align='center' class='mt-3'><tr>
<td align='center'>Total Width</td><td><input type='text' name='t_width' value='770' size='4'></td>
<td align='center'>Cell Height</td><td><input type='text' name='c_height' value='50' size='4'></td>
</tr>";
$body .= "<tr><td colspan='4' align='center' class='pt-3'>
<input type='submit' value='Display Printer Friendly Page' class='btn btn-primary btn-sm mx-1'/>
<input type='submit' value='Display HTML Code' class='btn btn-secondary btn-sm mx-1'/>
</td></tr></table></form>";
echo $body;
Test the calendar generator below. Enter notes or events into any date cell and generate a printer-friendly layout or clean HTML code structure.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Anthony Aniah | 30-04-2015 |
| Bless you for the codes. Very Good. | |
| Syed Zakir | 21-10-2015 |
| Respected sir ....... i want coding of calendar event on type day on month. Please send me i'm thank full to You. | |
10-06-2020 | |
| Hi, may i ask, for this PHP calendar, is it able to insert "number" to each date, add a new field which auto calculate the total number at bottom, and create a button to save it? Can this calendar insert into every user profile page which every user got their own calendar update? | |
24-06-2020 | |
| Yes, can be done, you need one member table where number against the date along with the member id is stored. While displaying you can read the numbers of the member with calendar date ( of a range or month ) and display them. Sum of these numbers can be displayed by just adding. These numbers are unique to the members. You can also give them edit facility. | |