Creating a calendar in PHP can be an enlightening exercise in date manipulation and presentation. Here's a comprehensive guide to building a simple PHP Calendar:
Before we delve into building the calendar, let's get acquainted with PHP's date and time functions:
If the month and year are not provided (e.g., via `GET` request), default to the current month and year.
$month = isset($_GET['month']) ? (int)$_GET['month'] : date('m');
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
2. Days in the Month and Starting Day We'll use PHP's date functions to find out how many days are in the selected month and what day of the week the month starts on.
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$startDay = date('N', mktime(0, 0, 0, $month, 1, $year));
We'll create an HTML table for our calendar:
echo '';
echo '';
echo '';
// Print empty cells until the first day of the month
for ($i = 1; $i < $startDay; $i++) {
echo '';
}
// Print the days
for ($day = 1; $day <= $daysInMonth; $day++, $startDay++) {
echo '';
// Start a new row after Sunday
if ($startDay % 7 == 0) {
echo '';
}
}
// If the month doesn't end on a Sunday, fill the remaining cells with blanks
while ($startDay <= 7) {
echo '';
$startDay++;
}
echo '';
echo 'MonTueWedThuFriSatSun' . $day . '';
You can add simple links to navigate to the previous and next months:
$prevMonth = $month - 1;
$prevYear = $year;
$nextMonth = $month + 1;
$nextYear = $year;
if ($prevMonth == 0) {
$prevMonth = 12;
$prevYear--;
}
if ($nextMonth == 13) {
$nextMonth = 1;
$nextYear++;
}
echo 'Previous';
echo 'Next';
You can use CSS to style your calendar, and even add functionality like highlighting the current day, marking events, etc. For further enhancements, you might want to consider adding AJAX to switch months without a page refresh or integrating a database to store events.
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here