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

Simple PHP Calendar

By , About.com Guide

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]

Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.