Simple DatePicker-like Calendar

A simple DatePicker-like calendar can be created using the PHP DateTime class and some basic HTML and CSS. Here is an example of how you might create a calendar for the current month:

$date = new DateTime();
$currentMonth = $date->format('m');
$currentYear = $date->format('Y');
$numDays = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);

echo '<table>';
echo '<tr>';
echo '<th>Sun</th>';
echo '<th>Mon</th>';
echo '<th>Tue</th>';
echo '<th>Wed</th>';
echo '<th>Thu</th>';
echo '<th>Fri</th>';
echo '<th>Sat</th>';
echo '</tr>';

for ($i = 1; $i <= $numDays; $i++) {
  $dayOfWeek = date('w', strtotime("$currentYear-$currentMonth-$i"));
  if ($i == 1) {
    echo '<tr>';
    for ($j = 1; $j <= $dayOfWeek; $j++) {
      echo '<td></td>';
  echo '<td>' . $i . '</td>';
  if ($dayOfWeek == 6 || $i == $numDays) {
    echo '</tr>';

echo '</table>';

Watch a course Learn object oriented PHP

This example creates a table with the days of the week as the headings, and the days of the current month as the table cells. It uses the PHP DateTime class to determine the current month and year, and the cal_days_in_month() function to determine the number of days in the current month. The strtotime() function is used to determine the day of the week for each day of the month, and the table is built using nested for loops.

You can style the table using CSS to make it look like a calendar.