Here is an example of how you can use PHP to draw the header of a calendar as an HTML table. Comments are included in the code:
<?php
//get today's date-time
$today=time();
//get the month to display, if no month to get, display this month
$This_Month=$_GET['Month'];
if ($This_Month=="")
$This_Month=date("F",$today);
//get the year to display, if no year to get, display this year
$This_Year=$_GET['Year'];
if ($This_Year=="")
$This_Year=date("Y",$today);
//set display date to the first day of the selected month and year
$First_Day=strtotime("$This_Month 1 $This_Year");
// the next month is guaranteed to be 3,456,000 seconds (40 days)
// from the first day of the selected month
$Plus_Month=$First_Day+3456000;
// the previous month is guaranteed to be 86,401 seconds (24:00:01 hrs)
// less than the first day of the selected month
$Minus_Month=$First_Day-86401;
// show the previous and next months-years in text format
$Next_Month=date("F",$Plus_Month);
$Prev_Month=date("F",$Minus_Month);
$Next_Year=date("Y",$Plus_Month);
$Prev_Year=date("Y",$Minus_Month);
// set the column width of each weekday in the calendar
$col_width="90";
// now we draw the Calendar header as the start of an HTML table
// we'll need to close it later with a </table> tag
echo "
<table border='1'>
<tr align='center' border='0'>
<td align='center' colspan='7'>
<font size='6'>$This_Month $This_Year</font>
</td>
</tr>
<tr>
<td colspan='2'>
<a href='?Month=$Prev_Month&Year=$Prev_Year'> << Previous Month</a>
</td>
<td colspan='3' align='center'>
<a href='?'>Reset to Current Month</a>
</td>
<td colspan='2' align='right'>
<a href='?Month=$Next_Month&Year=$Next_Year'>Next Month >> </a>
</td>
</tr>
<tr>
<td width='$col_width' align='center'>
Sunday
</td>
<td width='$col_width' align='center'>
Monday
</td>
<td width='$col_width' align='center'>
Tuesday
</td>
<td width='$col_width' align='center'>
Wednesday
</td>
<td width='$col_width' align='center'>
Thursday
</td>
<td width='$col_width' align='center'>
Friday
</td>
<td width='$col_width' align='center'>
Saturday
</td>
</tr>
";
?>
You could load this in a library as a PHP function and simply call it with one line. Then, you could write in the days as table colums and rows with your own information. Finally, you could close the table with an HTML </table> tag.
This is nice, in that it lets you progress from month to month, year to year. You could easily adapt this to control the look and feel with a cascading style sheet (CSS), if you wanted to pretty this up...
This is a pretty good example of the following:
- Using the time() function to get the current time
- Using the date() function to extract only the month or year
- Using the strtotime() function to set a date as a number
- Using the $GET[] variable to get information passed on the url
- Passing information on the URL for the $GET[] variable
Because I don't include the URL in the HTML <a href> statement, it assumes the url is the current page. The reset link would not work as an empty set of quotes, so I set it equal to the question mark (?). This then resets the calendar to the original page without any parameters passed on the URL.
On an odd note, advancing only 31 days caused the calendar to lock up going from October to November. Setting the advance to 40 days fixed the problem. Wierd, but it works.
I hope this helps some of the other newbies, besides myself, with understanding some coding better...