PHP Calendar

Creating and displaying calendars with PHP

๐Ÿ“† What is PHP Calendar?

PHP calendar functions help create dynamic calendars for web applications. The cal_days_in_month() function returns the number of days in a specific month and year.


<?php
// Get days in March 2025
$days = cal_days_in_month(CAL_GREGORIAN, 3, 2025);
echo "March 2025 has " . $days . " days";
?>
                                    

Output:

March 2025 has 31 days

Calendar Functions

๐Ÿ“…

Days in Month

Get number of days in a month

<?php
$days = cal_days_in_month(
  CAL_GREGORIAN, 2, 2024
);
?>
๐Ÿ“†

Calendar Info

Get calendar information

<?php
$info = cal_info(
  CAL_GREGORIAN
);
?>
๐Ÿ—“๏ธ

Month Calendar

Generate month calendar array

<?php
$cal = cal_from_jd(
  cal_to_jd(CAL_GREGORIAN, 3, 10, 2025),
  CAL_GREGORIAN
);
?>
โฐ

Date Conversion

Convert between calendar systems

<?php
$jd = cal_to_jd(
  CAL_GREGORIAN, 3, 10, 2025
);
?>

๐Ÿ”น Simple Calendar Display

Create a basic calendar display showing the current month. This example uses date functions to determine the month layout and displays it in a table.

<?php
$month = date('m');
$year = date('Y');
$monthName = date('F', mktime(0, 0, 0, $month, 1, $year));

echo "<h3>$monthName $year</h3>";
echo "<table border='1'>";
echo "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";

$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$firstDay = date('w', mktime(0, 0, 0, $month, 1, $year));

echo "<tr>";
for($i = 0; $i < $firstDay; $i++) {
    echo "<td></td>";
}

for($day = 1; $day <= $daysInMonth; $day++) {
    echo "<td>$day</td>";
    if(($day + $firstDay) % 7 == 0) {
        echo "</tr><tr>";
    }
}
echo "</tr></table>";
?>

Output:

March 2025

Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15

๐Ÿ”น Days in Month Function

The cal_days_in_month() function returns the number of days in a specified month and year. Useful for validating dates and creating calendars.

<?php
// Get days in different months
$jan = cal_days_in_month(CAL_GREGORIAN, 1, 2025);
$feb = cal_days_in_month(CAL_GREGORIAN, 2, 2025);
$febLeap = cal_days_in_month(CAL_GREGORIAN, 2, 2024);

echo "January 2025: " . $jan . " days<br>";
echo "February 2025: " . $feb . " days<br>";
echo "February 2024 (leap): " . $febLeap . " days<br>";

// Check current month
$currentMonth = date('n');
$currentYear = date('Y');
$daysThisMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
echo "This month has: " . $daysThisMonth . " days";
?>

Output:

January 2025: 31 days
February 2025: 28 days
February 2024 (leap): 29 days
This month has: 31 days

๐Ÿ”น Calendar Information

The cal_info() function returns information about a calendar system including month names, day names, and calendar properties for localization.

<?php
// Get Gregorian calendar info
$info = cal_info(CAL_GREGORIAN);

echo "Calendar: " . $info['calname'] . "<br>";
echo "Abbreviation: " . $info['calsymbol'] . "<br><br>";

echo "Months:<br>";
foreach($info['months'] as $num => $name) {
    echo "$num: $name<br>";
    if($num == 3) break; // Show first 3 months
}

echo "<br>Days:<br>";
foreach($info['abbrevdaynames'] as $num => $name) {
    echo "$num: $name<br>";
    if($num == 2) break; // Show first 3 days
}
?>

Output:

Calendar: Gregorian
Abbreviation: CAL_GREGORIAN

Months:
1: January
2: February
3: March

Days:
0: Sun
1: Mon
2: Tue

๐Ÿ”น Dynamic Month Selector

Create an interactive calendar that allows users to select different months and years. This example demonstrates building a month navigation system.

<?php
// Get month and year from URL or use current
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');

$monthName = date('F', mktime(0, 0, 0, $month, 1, $year));
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// Previous and next month links
$prevMonth = $month - 1;
$prevYear = $year;
if($prevMonth < 1) {
    $prevMonth = 12;
    $prevYear--;
}

$nextMonth = $month + 1;
$nextYear = $year;
if($nextMonth > 12) {
    $nextMonth = 1;
    $nextYear++;
}

echo "<a href='?month=$prevMonth&year=$prevYear'>&laquo; Previous</a> ";
echo "<strong>$monthName $year</strong> ";
echo "<a href='?month=$nextMonth&year=$nextYear'>Next &raquo;</a><br><br>";
echo "This month has $daysInMonth days";
?>

Output:

ยซ Previous March 2025 Next ยป

This month has 31 days

๐Ÿ”น Highlight Current Day

Enhance your calendar by highlighting the current day. This makes it easier for users to identify today's date in the calendar view.

<?php
$month = date('m');
$year = date('Y');
$today = date('j');
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);

echo "<h3>" . date('F Y') . "</h3>";
echo "<div style='display: flex; gap: 5px;'>";

for($day = 1; $day <= 10; $day++) {
    if($day == $today) {
        echo "<div style='background: #007cba; color: white; padding: 10px; border-radius: 5px;'>$day</div>";
    } else {
        echo "<div style='background: #f0f0f0; padding: 10px; border-radius: 5px;'>$day</div>";
    }
}
echo "</div>";
?>

Output:

March 2025

1
2
10
11

๐Ÿง  Test Your Knowledge

Which function returns the number of days in a month?