Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Calendar in PHP

Calendar in PHP

Creating a calendar in PHP can be a fun and useful feature, whether you want to display the current month, a full-year calendar, or even build a scheduling system. PHP provides many tools to manipulate dates and display them in a calendar format.

Let’s go over how to create a simple calendar using PHP!


?? Simple Calendar in PHP

1. Display the Current Month’s Calendar

We will use PHP's date() and cal_days_in_month() functions to get the current month's calendar, along with HTML to create a table-like structure for the calendar.


? Example: Basic Monthly Calendar

<?php// Get the current month and year$month = date('m');$year = date('Y');// Get the first day of the month and the total number of days in the month$first_day_of_month = strtotime("$year-$month-01");$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);$day_of_week = date('w', $first_day_of_month); // 0 (for Sunday) through 6 (for Saturday)echo "<h2>Calendar for " . date('F Y') . "</h2>";echo "<table border='1' cellpadding='5'>";echo "<tr>";// Days of the week headers$days_of_week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];foreach ($days_of_week as $day) {    echo "<th>$day</th>";}echo "</tr><tr>";// Print empty cells for the days before the 1st of the monthfor ($i = 0; $i < $day_of_week; $i++) {    echo "<td></td>";}// Print the days of the monthfor ($day = 1; $day <= $days_in_month; $day++) {    echo "<td>$day</td>";    $day_of_week++;    // If we reach the end of the week, start a new row    if ($day_of_week == 7) {        echo "</tr><tr>";        $day_of_week = 0;    }}// Fill in any remaining empty cells at the end of the monthif ($day_of_week != 0) {    for ($i = $day_of_week; $i < 7; $i++) {        echo "<td></td>";    }}echo "</tr>";echo "</table>";?>

? Explanation:

  1. date('m') and date('Y'): Get the current month and year.

  2. cal_days_in_month(): Get the number of days in the current month.

  3. date('w'): Get the day of the week for the 1st of the month (0 = Sunday, 6 = Saturday).

  4. <table>: Structure the calendar into a table with days of the week and day numbers.

  5. Loop through the days of the month, adding empty <td> tags for days before the 1st of the month and then filling the days after.

? Output:

  • Displays the current month's calendar in a table format.

  • The current month is displayed as the table header (e.g., January 2025).

  • The weekdays are displayed at the top (Sun, Mon, Tue, etc.).

  • Each day of the month is displayed within a <td> tag.


? Adding Features to Your Calendar

  1. Highlight the Current Day:You can easily highlight today’s date in the calendar.

    $today = date('j'); // Get today's day number (1 to 31)

    Then, add a condition inside the loop to check if the current $day matches $today and apply a CSS class or style.

    if ($day == $today) {    echo "<td style='background-color: yellow;'>$day</td>";} else {    echo "<td>$day</td>";}
  2. Navigate Between Months:You can create links or buttons that allow users to navigate between months, like Next and Previous month.

    echo "<a href='?month=" . ($month - 1) . "'>Previous</a>";echo "<a href='?month=" . ($month + 1) . "'>Next</a>";
  3. Dynamic Calendar with Events:

    • You can fetch events from a database and display them on the calendar.

    • You can display event descriptions in tooltips or popups when users hover over a specific day.

  4. Create a Yearly Calendar:You can loop over each month for the year and create a grid with all months.


Example of Enhanced Calendar with Navigation:

<?php// Get the current month and year or from URL$month = isset($_GET['month']) ? $_GET['month'] : date('m');$year = isset($_GET['year']) ? $_GET['year'] : date('Y');// Adjust for next/previous month navigationif ($month < 1) {    $month = 12;    $year--;} elseif ($month > 12) {    $month = 1;    $year++;}// Generate the calendar for the selected month$first_day_of_month = strtotime("$year-$month-01");$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);$day_of_week = date('w', $first_day_of_month); // Day of the week for the first day// Display the navigationecho "<a href='?month=" . ($month - 1) . "&year=$year'>Previous</a> | ";echo "<a href='?month=" . ($month + 1) . "&year=$year'>Next</a>";// Display the calendar tableecho "<h2>" . date('F Y', strtotime("$year-$month-01")) . "</h2>";echo "<table border='1' cellpadding='5'>";echo "<tr>";$days_of_week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];foreach ($days_of_week as $day) {    echo "<th>$day</th>";}echo "</tr><tr>";// Print empty cells before the 1st of the monthfor ($i = 0; $i < $day_of_week; $i++) {    echo "<td></td>";}// Print the days of the monthfor ($day = 1; $day <= $days_in_month; $day++) {    echo "<td>$day</td>";    $day_of_week++;    if ($day_of_week == 7) {        echo "</tr><tr>";        $day_of_week = 0;    }}if ($day_of_week != 0) {    for ($i = $day_of_week; $i < 7; $i++) {        echo "<td></td>";    }}echo "</tr>";echo "</table>";?>

? Additional Features:

  • Event Integration: Connect to a database to fetch events for a specific day.

  • Time Slot Selection: For scheduling or booking appointments.

  • CSS Styling: Make the calendar visually appealing by adding CSS classes to each date, highlighting weekends, etc.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql