Simple Calendar Tutorial
This is a simple tutorial to make calendar display in your PHP webpage. basically you would need an PHP enable webhosting to run this code.
The code were hacked from the original calendar script source, at keithdevens.com
make an cal.php
<?php
class Calendar {
function generate_calendar($year, $month, $day_func = NULL, $day_heading_length = 3) {
$first_of_month = mktime (0,0,0, $month, 1, $year);
static $day_headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$maxdays = date('t', $first_of_month); #number of days in the month
$date_info = getdate($first_of_month); #get info about the first day of the month
$month = $date_info['mon'];
$year = $date_info['year'];
$calendar = "<br /><table width='100%'>";
$calendar .= "<tr>
<td colspan='7' class='navi'>$date_info[month] $year</td>
</tr>";
$calendar .= "<tr>
<td style='font-size:10px;'>Sun</td>
<td style='font-size:9px;'>Mon</td>
<td style='font-size:9px;'>Tue</td>
<td style='font-size:9px;'>Wed</td>
<td style='font-size:9px;'>Thu</td>
<td style='font-size:9px;'>Fri</td>
<td style='font-size:9px;'>Sat</td>
</tr>";
$calendar .= "<tr>";
$class = "";
$weekday = $date_info['wday']; #weekday (zero based) of the first day of the month
$day = 1; #starting day of the month
#take care of the first "empty" days of the month
if($weekday > 0){$calendar .= "<td colspan='$weekday'> </td>";}
#print the days of the month
while ($day <= $maxdays) {
if($weekday == 7) { #start a new week
$calendar .= "</tr><tr>";
$weekday = 0;
}
$linkDate = mktime (0,0,0, $month, $day, $year);
if((($day<10 AND "0$day"==date('d')) OR ($day>=10 AND "$day"==date('d'))) AND (($month<10 AND "0$month"==date('m')) OR ($month>=10 AND "$month"==date('m'))) AND $year==date('Y'))
$class="caltoday";
else {
$d = date('m/d/Y', $linkDate);
$class = "cal";
}
$link = "dates.php?date=$linkDate";
$calendar .= "<td class='$class'>$day</td>";
$day++;
$weekday++;
}
if($weekday != 7) {
$calendar .= "<td colspan='" . (7 - $weekday) . "'> </td>";
}
return $calendar . "</tr></table>";
}
}
?>
It is useful if you use external Cascading Style Sheet. Just add the following line in your .css file.
.navi {
padding:5px 5px 5px 0px;
font-family: Tahoma;
font-size: 16px;
color:#999999;
}
.cal {
color:#40516D;
}
.caltoday {
font-family: Tahoma;
font-size: 14px;
color:#E26C00;
}Let say you want to use it at index.php
<?php
include "cal.php";
$MCal = new Calendar;
print $MCal ->
generate_calendar(date('Y'), date('m'), $day_func = NULL, $day_heading_length = 3);
?>










» kkdoirj@invalid.com:
on 13-July 2006 at 21:15 PM | Quote
doesn't work as coded
crynobone:
on 13-July 2006 at 22:35 PM | Quote
The problem has been noted and solved (I had tested the code just now and it work). The bug were from my AJAX function with ignore symbol from during form validation (which I noted here). Everything should be fine by now. Thank for the head-up.
» vanalmelo@yahoo.com:
on 23-July 2006 at 02:45 AM | Quote
hmm.. I get this error:
Fatal error: Cannot redeclare class calendar in /home/rockaway/public_html/php/cal.php on line 66
can you help?
crynobone:
on 23-July 2006 at 10:23 AM | Quote
How can you have line 66 when there only 56 line in cal.php. Follow the file structure correctly.
» Eric:
on 19-September 2006 at 06:36 AM | Quote
Is it possible to create a link for certain days?
If so, how?
crynobone:
on 19-September 2006 at 07:53 AM | Quote
You can modify this code.
$calendar .= "<td class='$class'>$day</td>";
to something like
$link = "page.php?date=$linkDate";$calendar .= "<td class='$class'><a href='".$link."'>$day</a></td>";
» Eric:
on 22-September 2006 at 06:13 AM | Quote
Thanks!
But this creates a link for all the days of a month. What about a link for only one day?
crynobone:
on 22-September 2006 at 08:17 AM | Quote
You have to be creative to add additional variable and make if else condition check to do that.