Very simple PHP calendar

Many of the calendar scripts out there are un-necessarily complicated & weighty.

If you want a tiny calendar script that is easy to understand and can be adapted to a variety of purposes, here’s one we wrote for our Doozy CMS that might be a good starting point for you.

The PHP:

Usage: echo calendar(10, 2014); // month, year[, timezone] (enter the timezone using standard PHP timezone strings)

function calendar($m, $y, $timezone = 'Pacific/Auckland') { // set your default timezone here
  $date = new DateTime('now', new DateTimeZone($timezone));
  $today = array('d' => $date->format('d'), 'm' => $date->format('m'), 'y' => $date->format('Y'));
  $date->setDate($y, $m, 1);
    $calendar = '
    <table class="calendar"><tr class="month-heading"><th><a class="prev" href="?m='.$date->modify('-1 month')->format('n').'&amp;y='.$date->format('Y').'">&#9664;</a></th><th colspan="5" class="big">' . $date->modify('+1 month')->format('F Y') . '</th><th><a class="next" href="?m='.$date->modify('+1 month')->format('n').'&amp;y='.$date->format('Y').'">&#9654;</a></th></tr>
    <tr class="week-heading"><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr>
    ';
    $month = array('this_month_start_dow' => $date->modify('-1 month')->format('w'), 'this_month_days' => $date->format('t'), 'prev_month_days' => $date->modify('-1 month')->format('t'));
  $days_arr = array();
  $day_ctr = -1;

  // previous month
  if ($month['this_month_start_dow'] > 0) {
    for ($i = $month['this_month_start_dow'] - 1; $i >= 0; $i--) {
      $day_ctr += 1;
      $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day other-month' : 'other-month'; // Sat/Sun
      $days_arr[] = '<td class="' . $class . '">' . ($month['prev_month_days'] - $i) . '</td>';
    }
  }

  // current month
  for ($i = 1; $i <= $month['this_month_days']; $i++) {
    $day_ctr += 1;
    $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day' : ''; // Sat/Sun
    $class .= ($i == $today['d'] && $m == $today['m'] && $y == $today['y']) ? ' today' : ''; // today
    $days_arr[] = '<td class="' . $class . '">' . $i . '</td>';
  }

  // next month
  if (count($days_arr) % 7 != 0) {
    for ($i = 1; $i <= count($days_arr) % 7; $i++) {
      $day_ctr += 1;
      $class = ($day_ctr % 7 == 0 || $day_ctr % 7 == 6) ? 'weekend-day other-month' : 'other-month'; // Sat/Sun
      $days_arr[] = '<td class="' . $class . '">' . $i . '</td>';
    }
  }

  // create the grid
  foreach ($days_arr as $k => $day) {
    $calendar .= (($k) % 7 == 0 ? '</tr><tr>' : '') . $day;
  }

  $calendar .= '</tr></table>';
  return $calendar;
}

Add a bit of CSS

<style>
  .calendar {width:220px;background:#fdfdfd;font:14px arial; color:#644; border:1px solid #dcdcdc; border-collapse:collapse;}
  .month-heading {text-align:center; height:28px; line-height:28px; background:#b53934; color: #fff;}
  .big {font-size:120%;}
  .week-heading {font-size:11px; height:20px; line-height:20px; background:#e6e7ec; text-align:center;}
  td {width:14.2857%;text-align:right;padding:4px!important; border:1px solid #dcdcdc;}
  .other-month {color: #dcdcdc;}
  .weekend-day {background:#fafafa;}
  .today {background:#a6daf0;}
</style>

And there you have it – an easy to use, flexible and customisable calendar. Using the above css should result in something that looks like this:

calendar

Many other options can be easily added, for example in our Doozy CMS events module we include clickable links on days that have events along with an optional summary of events for the selected month below the calendar.

Hope it’s of use to you…