Posts Tagged ‘PHP’

Build Calendar with PHP

May 5th, 2010

CLICK HERE TO SEE ONLINE DEMO

For my last project I required to use calendar in PHP. I searched of it online and there were lots of them. But all of them were complicated, packed with lots of features (appointments etc.) and lacks examples. There were calendar classes but of no use. So, instead of searching, I decided to create code for calendar myself in PHP. Working on logic I used for creating calendar, I didn’t find it hard to build it. On the other hand, it was way too easy to create it than it actually looks like. No doubts, I was very much helped by many date and time pre built functions provided by PHP. Hence I am providing you with the code that creates and displays calendar. The outcome of the code is below (Apologies for my poor styling skills),

Screenshot for php calendar

Screenshot for php calendar

 

The table head section shows the current month and year along with 2 links (Previous & Next Month) for navigating to different months. Below that there are columns for various days in week. The table body section consists of 6 rows and 7 columns. The reason behind 7 columns is easy, for 7 days in a week. The logic behind 6 rows lies in the fact that even if the 1 of the month (with 31 days) starts from the last day of week, the last day won’t go beyond 2 column of 6th row. I used some CSS classes like “.holiday” for Sundays “.day” for normal days of month and no class for blank <td>.

The logic starts with two loops with outer loop running 6 times (for weeks) and inner 7 times (for week days). In first loop we will put <td>&nbsp;</td> till it encounter the 1 day of the month (variable – $skip). Both the loop will run fine till the days in that month (variable – $daysInMonth). If it’s Sunday then we will add “.holiday ” class in <td> otherwise “. day ” class along with date. The next and previous month links is formed with adding year + month as string (e.g. 201005 for May, 2010) and pass it as parameter to the page itself. The rest of the code is self-explanatory. Below is the PHP code for the calendar ( or DOWNLOAD CODE)

<?php

// if ym is set, i.e. somebody clicked on next or previous months link
if(isset($_GET["ym"]))
{
$year = (int)substr($_GET["ym"], 0, 4);
$month = (int)substr($_GET["ym"], 4, 2);
}
else    // otherwise take current month & year
{
$month = date(“m”, mktime(0,0,0,date(‘m’),1,date(‘Y’)));
$year = date(“Y”, mktime(0,0,0,date(‘m’),1,date(‘Y’)));
}

$skip = date(“w”, mktime(0,0,0,$month,1,$year)); // days to skip in 1 row of week.
$daysInMonth = date(“t”, mktime(0,0,0,$month,1,$year));    // total number of dates in the month.
$calendar_head = ”;    // for calendar head
$calendar_body = ”;    // for calendar boday
$day = 1;    // For date in calendar

for($i = 0; $i < 6; $i++) // Outer loop for weeks
{
$calendar_body .= ‘<tr>’;    // start row tag
for($j = 0; $j < 7; $j++)    // Inner loop for week days
{
if(($skip > 0)||($day > $daysInMonth)) // display blank till 1 day of month or after total numnber of days in that month
{
$calendar_body .= ‘<td>&nbsp;</td>’;
$skip–;
}
else
{
if($j == 0)    // if its Sunday then add class holiday
$calendar_body .= ‘<td>’.$day.’</td>’;
else    // otherwise add day class
$calendar_body .= ‘<td>’.$day.’</td>’;

$day++; // Increment $day
}

}    // inner loop closes
$calendar_body .= ‘</tr>’; // end row tag
} // outer loop closes

// Calendar head section
$calendar_head = ‘
<tr>
<th colspan=”2″><a href=”?ym=’.date(“Ym”, mktime(0,0,0,$month-1,1,$year)).’”>&laquo; Previous Month</a></th>
<th colspan=”3″>’.date(“F, Y”, mktime(0,0,0,$month,1,$year)).’</th>
<th colspan=”2″><a href=”?ym=’.date(“Ym”, mktime(0,0,0,$month+1,1,$year)).’”>Next Month &raquo;</a></th>
</tr>
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>’;
// PHP code for calendar ends

?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>A Calendar Built in PHP</title>

<style type=”text/css”>

#calendar tbody tr{ height:100px; }
#calendar td{ width:100px; }
#calendar th{background-color:#CCCC99;}
.day{ background-color:#CCFFCC; }
.holiday{ background-color:#FFCC66; }

</style>

</head>

<body>

<h2>A Calendar Built in PHP</h2>

<!– Table to display calendar –>
<table id=”calendar” width=”710″ border=”1″ cellspacing=”0″ cellpadding=”5″>
<thead>
<?php echo $calendar_head; ?>
</thead>
<tbody>
<?php echo $calendar_body; ?>
</tbody>
</table>
<!– Table to display calendar –>

</body>
</html>

The code is pretty easy to understand and you can figure out where to put your code to display your required info in the calendar. I used very basic CSS in this which you can style it as you wish. Integrate it according to your requirements. Hope it would help you guys. Your feedback and suggestions are always welcomed.