php Calendar Code

Here is a simple PHP calendar code that you can use to display a calendar on your website:

<?php

// Get current month and year
$month = date('n');
$year = date('Y');

// Get the first day of the month
$firstDay = date('w', strtotime("first day of $year-$month"));

// Get the total number of days in the month
$numDays = date('t', strtotime("$year-$month"));

// Get the name of the month
$monthName = date('F', strtotime("$year-$month"));

// Create an array of the days of the week
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

// Create the HTML for the calendar
echo '<table>';
echo '<tr>';
foreach($days as $day) {
  echo "<th>$day</th>";
}
echo '</tr>';

// Create the blank cells at the beginning of the month
for($i=0; $i<$firstDay; $i++) {
  echo '<td></td>';
}

// Create the cells for the days of the month
for($i=1; $i<=$numDays; $i++) {
  // If it's the first day of the week, start a new row
  if(($i+$firstDay-1) % 7 == 0) {
    echo '</tr><tr>';
  }
  // Add the day to the cell
  echo "<td>$i</td>";
}

// Create the blank cells at the end of the month
for($i=0; ($i+$firstDay+$numDays) % 7; $i++) {
  echo '<td></td>';
}

echo '</tr>';
echo '</table>';

?>

This code will display a calendar for the current month. You can modify the $month and $year variables to display a different month and year.

Leave a Comment