1. Home
  2. Computing & Technology
  3. PHP / MySQL

Simple PHP Calendar

By Angela Bradley, About.com

4 of 5

Days of the Month

//sets the first day of the month to 1
$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;
}
}
Now 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.

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).

[See working calendar example]

4 of 5

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. PHP Calendar - Simple PHP Calendar - PHP Calendar Tutorial

©2008 About.com, a part of The New York Times Company.

All rights reserved.