View Full Version : Looking for PHP code to build a calendar from a MySQL DB

05-22-2008, 06:04 PM
I have a database of events that I want to display in traditional calendar format (ie a table for each month with columns for days of week and cell for each day) In each cell I want to display the event titles for that day as links to a detail page for the event.

My DB has columns for:

Start Date
End Date
Event Title
Event Details

Can anyone point me to some open source or freeware code to handle this?


05-22-2008, 06:24 PM

I think that's exactly what you're looking for. Each one of those calendars was printed with a function I made called print_cal which takes a month and year as parameters.

function print_cal($month,$year) {
$cal_names = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$cal_last = date('t',mktime(0,0,0,$month,1,$year)) ;
$cal_start = date('w',mktime(0,0,0,$month,1,$year)) + 1;
$calendar = array();
$start_date = mktime(23,59,59,$month,0,$year);
$end_date = mktime(0,0,0,$month+1,1,$year);
// Confusing query, but it's just selecting all of events with start or end times inside of
// the month we're targeting.
$query = "SELECT * FROM `calendar` WHERE ((`start_time`>'$start_date' AND `start_time`<'$end_date' ) OR (`end_time`>'$start_date' AND `end_time`<'$end_date'))";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
$temp_id = $row['index'];
$temp_start = $row['start_time'];
$temp_end = $row['end_time'];
$temp_who = $row['who'];
while($temp_start<=$temp_end && $temp_start<=mktime(23,59,59,$month,cal_days_in_month(CAL_GREGORIAN,$month,$year),$year))
$temp_date = date("d",$temp_start);
$calendar[$temp_date] = $temp_who;
$temp_start = mktime(23,59,59,$month,$temp_date+1,$year);
print "<br/>";
print '<b>'.$cal_names[$month-1].", $year</b>";
print '<table width="150" border="0" id="old_table"><tr>';
print '<tr><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';
if ($cal_start - 1 != 0) {
print "<td class='calendar' colspan=". ($cal_start-1) .">&nbsp</td>";
for ($j=1;$j<=$cal_last;$j++)
if ($j<10)
$j = '0'.$j;
if(isset($calendar[$j])) {
print "<td width=20 class=\"".$calendar[$j]."\"><div class=\"tip\"onmouseover=\"tooltip('$calendar[$j]');\" onmouseout=\"exit();\">$j</div></td>";
} else {
print "<td width=\"20\">$j</td>";
if ($cal_pos % 8 == 0)
print "</tr><tr>";
print "</tr></table>";

The code's not too clean since I hijacked the code from another calendar I made (without the functionality of the database).

05-22-2008, 08:37 PM
Thanks - that's a nice looking calendar.

I need to have it display a cell for each day with the events for that day listed in the cell.

05-23-2008, 01:47 PM
If you were using my code to do that, you would have to store a (reference to) the information you wanted to display inside of your $calendar array (you'd change this in that while loop). You'll see that I store the name of each of the visitors in $calendar and then echo it in the for loop below. You could store whatever information you wanted here (including other columns gathered from your SQL query).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum