//sets the first day of the month to 1Now we need to fill in the days of the month. We do this with another while loop, but this time we are counting up to the last day of the month. Each cycle echos a table detail with the day of the month, and it repeats until we reach the last day of the month.
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
Our loop also contains a conditional statement. This checks if the days of the week have reached 7, the end of the week. If it has, it starts a new row, and resets the counter back to 1 (the first day of the week).

