PHP Calendar Month Function

I decided to start working on a follow-up project to my previous Vlad Tracker. The first version of it is pretty simple. It just lists all of his days off vertically. Next step of course is to display everything in a calendar view. The first version also focused on simplicity, including all of the scripts & styles inline so that it could be just saved on the hard drive without any messy additional files. This time it is in PHP because of the convenient date manipulation functions.

I needed a function that would generate a calendar month. After a bit of searching on google, I didn’t find any code that did what I wanted. Everything already generated html with calendar-type widgets. So I wrote my own.

PHP function getCalendarArray takes 2 arguments; a date and a number representing day of the week. The date can be either a string representing a date (as accepted by DateTime object) or a DateTime object itself. The second parameter is an integer 0-6 representing the first day of the week. 0 for Sunday, 1 for Monday … 6 for Saturday. The function returns an array where each element is an array representing a date. It’s meant to fit in a square so the first and last few days will be of the previous and next month respectively. The outer array is numerically indexed, the outer array contains the following string indexes: day, month, year, specified. Specified is a boolean representing the specific date that is passed in the first argument.

Function prototype:

function getCalendarArray([$date [, $dao_adj]])

Sample abridged return value:

array
  0 => 
    array
      'day' => int 28
      'month' => int 8
      'year' => int 2011
      'specified' => boolean false
  1 => 
    array
      'day' => int 29
      'month' => int 8
      'year' => int 2011
      'specified' => boolean false
  2 => 
    array
      'day' => int 30
      'month' => int 8
      'year' => int 2011
      'specified' => boolean false
  3 => 
    array
      'day' => int 31
      'month' => int 8
      'year' => int 2011
      'specified' => boolean false
  4 => 
    array
      'day' => int 1
      'month' => int 9
      'year' => int 2011
      'specified' => boolean false
  5 => 
    array
      'day' => int 2
      'month' => int 9
      'year' => int 2011
      'specified' => boolean false
  6 => 
    array
      'day' => int 3
      'month' => int 9
      'year' => int 2011
      'specified' => boolean false

Basically this functions provides data on how to render the calendar but doesn’t have any of the rendering code. Making it perfect for integration into custom calendar projects. Feel free to use it however you want!

View the full source, with comments and license.

Comments are closed.