I would like to know if this is a good way to generate a calendar for the given month? If there's anything I can do to dramatically increase the page generation time, or even optimize the HTML?
I'm here to readily learn.
Demo :
http://saney.ath.cx:8080/nutricraze.php
Code (without stylesheet or javascript *
See Demo*):
<?php
// create an "artificial" time, based on an offset, for graceful extending/debugging
$offset = 0;
$my_time = time() + $offset;
?>
<h3>Calendar Log - <?php echo date('F Y', $my_time); ?></h3>
<table class="calendar" border="0" cellpadding="0" cellspacing="1">
<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>
<?php
$days = date('t', $my_time); // days in the month
$day = date('j', $my_time); // current day
$day_of_week = date('w', $my_time); // current "day of week"
// calculate what "day of week" the month starts on
$month_start = $day_of_week - $day%7 + 1;
if ($month_start < 0)
$month_start = 7 + $month_start;
$this_day = 0;
$this_day_of_week = 0;
// second clause is to ensure the row ends after the days are finished
while ($this_day < $days || $this_day_of_week != 0)
{
// start row
if ($this_day_of_week == 0)
echo '<tr>';
// change day
if ($this_day == 0)
{
// only change to the first day when it's on the "day of week" the month starts on
if ($this_day_of_week == $month_start)
$this_day ++;
}
else
$this_day ++;
// perform output
// first clause is to create blanks before the first day (0th day)
// second clause is to create blanks after the month is over
if ($this_day == 0 || $this_day > $days)
echo '<td class="na"> </td>';
else if ($this_day == $day)
echo '<td class="now" onclick="o('.$this_day.')">'.$this_day.'</td>';
else
echo '<td onclick="o('.$this_day.')">'.$this_day.'</td>';
// change "day of week"
$this_day_of_week ++;
if ($this_day_of_week == 7)
{
// end row
echo '</tr>';
$this_day_of_week = 0;
}
}
?>
</table>