This page demonstrates how to generate a calendar for the current month as a table.

View the source to see the actual code. Here's the basic idea:

  • Get the current day of the month (DOM) and the current day of the week (DOW).
  • Calculate the date for the first sunday of the first row. This might be negative! Initialize a counter to that value.
  • Use a loop for each row of the table
  • Use a loop for each column of each row
  • For each cell, if the counter is a valid date number, print the counter, otherwise make the cell blank. Always increment the counter.
  • If the counter equals DOM, make the day bold.