Page 1 of 1

Making a Calendar in PHP Rate Topic: -----

#1 Auzzie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 43
  • View blog
  • Posts: 573
  • Joined: 20-January 09

Posted 15 January 2010 - 09:48 AM

Online calendars can be useful and can vary in complexity from a simple calendar to a full blown online booking system. In this tutorial i am going to cover the former of the two.
First thing that needs to be done is to work out what information we will need to collect and display. For this simpler system you would only need three pieces of information. 1) What day it is today 2) The first day of the month 3) The last day of the month.
The function that I use for retrieving all of the needed date information is PHP’s built in getdate() function. This function only has one parameter which is optional and that is the timestamp of the date you want. If you don’t use a parameter like we will in this tutorial then you will have the following array available to you.
Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)


Another very useful function that we will use is mktime() which gets the Unix timestamp for a date based on the parameters passed to it. We are going to use this function along with getdate() to work out what day is the first and last day of the month. The following example is a way in which we can do this by using the next months "zero day".
<?php
$now = getdate();
$monthStart = getdate(mktime(0, 0, 0, $now['mon'], 1, $now['year']));
$monthEnd = getdate(mktime(0, 0, 0, $now['mon'] + 1, 0, $now['year']));
?>


Now that we have all of this needed information it is time to actually create the calendar, now i know you a thinking oh that is easy we need seven columns and four rows that count from one up to thirty or thirty-one… If that is you then you are wrong.
The reason you are wrong is that over the space of a year there is fifty-two weeks, and not every month is spread over four of them, this is because not every month starts on the same day. The information that we know is consistent regardless of the month is that there will be seven columns and that we will need a header line for the month and year information, as well as needing another line to hold the name of each day of the week. Below is an example of the html.
<html>
<head>
<title>
My Calendar viewing < ?php echo($now['month'] . ' ' . $now['year']); ?>
</title>
</head>
<body>
<table>
<tr>
<th colspan="7">
<?php echo($now['month'] . ' ' . $now['year']); ?>
</th>
</tr>
<tr>
<td>
Monday
</td>
<td>
Tuesday
</td>
<td>
Wednesday
</td>
<td>
Thursday
</td>
<td>
Friday
</td>
<td>
Saturday
</td>
<td>
Sunday
</td>
</tr>


Now that we have created the top of the calendar table, it is time to start creating each of the weeks needed for the current month. As we said earlier not all months start on a Monday so we will need a couple of lines of code to find out what day is the first day of the month, then populate that cell with the number 1.
Let’s make things a little easier by starting off creating the first week. We are going to do this is by using the array we stored in $monthStart. This array is going to follow the same format as our first example. We will need just the value in the wday parameter. The following example will show you the best way in which to use that information to present the outcome that we need.
<tr>
<?php
for($counter = 1; $counter < $monthStart['wday']; $counter++)
{
?>
<td>
 
</td>
< ?php
}
$today = 0;
for($counter = $monthStart['wday']; $counter <= 7; $counter++)
{
$today ++;
?>
<td>
$today
</td>
< ?php
}
?>
</tr>


As you are able to see from the code example, we use two for loops, one to create blank squares that precede the first day of the month, and the other to complete the full week for us. All that is left is the easier task of completing the month. So all we need to do is work out how many full weeks we have gone through as well as actually filling out the table.
<?php
$totalWeeks = floor(($monthEnd['mday'] - $today) / 7);
for($counter = 0; $counter < $totalWeeks; $counter++)
{
echo '<tr>';
for($i = 0; $i < 7; $i++)
{
$today ++;
echo "<td>$today</td>";
}
echo '</tr>';
}
if($today < $monthEnd['mday'])
{
echo '<tr>';
for($i = 0; $i < 7; $i++)
{
$today++;
if($today <= $monthEnd['mday'])
{
echo "<td>$today</td>";
} else {
echo '<td> </td>';
}
}
echo '</tr>';
}
?>


There you have it, you have finally completed the entire month for the calendar. Some ideas for expanding the calendar would be to possibly add a link either side of the month that could change the month to either one month in the past or one month forward. Another would be to hook it up to a database so you could display events for the days displayed. Finally here is a full listing of the code.
<?php
$now = getdate();
$monthStart = getdate(mktime(0, 0, 0, $now['mon'], 1, $now['year']));
$monthEnd = getdate(mktime(0, 0, 0, $now['mon'] + 1, 0, $now['year']));
?>
<html>
<head>
<title>
My Calendar viewing < ?php echo($now['month'] . ' ' . $now['year']); ?>
</title>
</head>
<body>
<table>
<tr>
<th colspan="7">
< ?php echo($now['month'] . ' ' . $now['year']); ?>
</th>
</tr>
<tr>
<td>
Monday
</td>
<td>
Tuesday
</td>
<td>
Wednesday
</td>
<td>
Thursday
</td>
<td>
Friday
</td>
<td>
Saturday
</td>
<td>
Sunday
</td>
</tr>
<tr>
< ?php
for($counter = 1; $counter < $monthStart['wday']; $counter++)
{
?>
<td>
 
</td>
< ?php
}
$today = 0;
for($counter = $monthStart['wday']; $counter <= 7; $counter++)
{
$today ++;
?>
<td>
$today
</td>
< ?php
}
?>
</tr>
< ?php
$totalWeeks = floor(($monthEnd['mday'] - $today) / 7);
for($counter = 0; $counter < $totalWeeks; $counter++)
{
echo '<tr>';
for($i = 0; $i < 7; $i++)
{
$today ++;
echo "<td>$today</td>";
}
echo '</tr>';
}
if($today < $monthEnd['mday'])
{
echo '<tr>';
for($i = 0; $i < 7; $i++)
{
$today++;
if($today <= $monthEnd['mday'])
{
echo "<td>$today</td>";
} else {
echo '<td> </td>';
}
}
echo '</tr>';
}
?>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1