I built a calendar from scratch a while back now for a project I was working on at the time:
I first setup an array for weekdays:
$weekdays = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
Then using getdate and mktime I would find out what day the 1st of the month in question landed on. So if you’re building a 7 cell wide grid (weekdays) it’s easy to plot the start of the month against the weekdays along the top of your calendar.
$date = getdate (mktime(1, 1, 1, $month, 1, $year));
$startWeek = $date['wday'];
Then I found out how many days were in the month:
$monthDays = date ("j", mktime(1,1,1,$month+1, 0, $year));
And used this information to build a table.
Hope this helps. Probably not the most efficient way to do it, but it did the job back 1yr ago when I tackled the project.
This was the output of the calendar specific part:
