Well, let's take this in pieces, shall we? I'm going to use English days and months, so you'll need to convert it to your own language.
Since you are using a tabular format, this is going to make it pretty easy. To start with, let's assume you've already declared your HTML table and concentrate on what's in it.
The headers are going to be handled by a simple for() loop. Given that a month crosses no more than 5 weeks, we can lay out the headers like so:
echo <tr>\r\n";
echo "<th> </th>"; // blank header to compensate for row labels
for ($i=1;$i<=5;$i++) // do this 5 times...
echo "<th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th>\r\n";
echo "<th>Su</th><th>Mo</th>\r\n"; // if the first falls on Sat, we could need 37 boxes
echo "</tr>\r\n";
Next, we will build a numeric array of the months:
$month=array();
$month[1]="Jan";
$month[2]="Feb";
$month[3]="Mar";
$month[4]="Apr";
$month[5]="May";
$month[6]="Jun";
$month[7]="Jul";
$month[8]="Aug";
$month[9]="Sep";
$month[10]="Oct";
$month[11]="Nov";
$month[12]="Dec";
Let's do a generic days of the month array...
$days_of_month=array();
$days_of_month[1]=31;
if (date("Y")%4==0) $days_of_month[2]=29;
else $days_of_month[2]=28;
$days_of_month[3]=31;
$days_of_month[4]=30;
$days_of_month[5]=31;
$days_of_month[6]=30;
$days_of_month[7]=31;
$days_of_month[8]=31;
$days_of_month[9]=30;
$days_of_month[10]=31;
$days_of_month[11]=30;
$days_of_month[12]=31;
Let's next determine what day of the week each month starts on:
$year=date("Y");
$first_of_month=array();
for ($i=1;$i<=12;$i++) // do this for each of the 12 months
{
$timestamp=strtotime($month[$i]." 1 ".$year);
$first_of_month[$i]=date("w",$timestamp); // day of the week as a number (0-6)
}
Next, we need to get the current date values:
$day=date("j");
$month=date("n");
Now that we have set up all our arrays and figured out all the important information, we can build the individual rows:
for($i=1;$i<=12;$i++)
{
echo "<tr>\r\n";
echo "<td>$month[$i]</td>"; // creating the row label
//next, we determine if the month is past, present or future...
if ($i<$month) // if the month is passed...
{
for ($j=1;$j<=37;$j++) echo "<td> </td>";
echo "\r\n"; // new line for easier reading
} // end if($i<$month)
elseif ($i>$month) // month is in the future
{
$count=0; // count of up to 37 day blocks
for ($j=1;$j<=$first_of_month[$i];$j++) // number of blank spaces before...
{
echo "<td> </td>";
$count++; // increment counter
}
for ($j=1;$j<=$days_of_month[$i];$j++) // number of date boxes
{
echo "<td>$j</td>";
$counter++;
}
for ($j=1;$j<=(37-$counter);$j++) // end boxes
{
echo "<td> </td>";
}
echo "\r\n"; // insert line break for better readability
} // end elseif ($i>$month)
else // current month
{
$counter=0;
for ($j=1;$j<=($first_of_month[$i]+$day-1);$j++) // preceding blanks
{
echo "<td> </td>";
$counter++;
}
echo "<td><font color=\"red\">$day</font></td>"; // today's day in red
$counter++;
for ($j=$day+1;$j<=$days_of_month[$i];$j++) //remaining days of month
{
echo "<td>$j</td>";
$counter++;
}
for ($j=1;$j<=(37-$counter);$j++) // remaining blanks
{
echo "<td> </td>";
}
echo "\r\n"; // line break
} //end else -- current month
echo "</tr>\r\n";
}// end of for($i=1;$i<=12;$i++)
Then, close your table and continue. This code is untested, so there may be some bugs in there, but it will give you an idea of how it works. By breaking it into smaller, more manageable pieces, we find that it isn't so difficult to build.
Hope this helps