Calendar Generator

The simplest way to create a calendar in a web page is using a table. The difficult part is in entering all the dates into the correct places in the table so as to create the calendar you require for the correct month and year. Well with a simple PHP function we can do away with all the complexity and have the table automatically generated with the correct days filled in in the correct positions in the table for whichever month it is we are trying to create.

The following function accepts two parameters containing the month any year you want to generate the calendar for and it returns the HTML for the entire table containing the calendar for that month. This makes it as flexible as possible as to how we can use the function. Note that the function does not contain any validation for the month and year fields passed to it so you'll need to ensure that the values passed are valid and add any necessary validation if you are going to allow the values to be entered via a form.

// calendar function copyright Stephen Chapman
// Felgall Pty Ltd 31st December 2008
function calendar($m, $y) {$cal = mktime(0,0,0,$m,1,$y);
$moy = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$dow = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$weekday = date('w',$cal); $month = date('n',$cal); $year = date('Y',$cal); $today = date('j',$cal); $daysInMonth = date('t',$cal);
$h = '<table border="0" cellspacing="0" cellpadding="0"';
$h .= ' class="cal"><tr><th colspan="7">'.$moy[$month-1];
$h .= '&nbsp;'.$year.'</th></tr>'."\n".'<tr>';
for($dex=0; $dex < 7; $dex++) {$h .= '<th>'.$dow[$dex].'</th>';}
$h .= '</tr>'."\n".'<tr>'; $day2 = $today;
for ($dex = $today; $dex > 6; $dex -=7) $day2 = $dex;
$weekday -= $day2 - 1; while ($weekday < 0) $weekday += 7;
for($dex=0; $dex < $weekday; $dex++) $h .= '<td>&nbsp;</td>'; for($dex=1; $dex <= $daysInMonth; $dex++) {if($weekday == 7) {
$h .= '</tr>'."\n".'<tr>'; $weekday = 0;} $h .= '<td>'.$dex.'</td>'; $weekday++;} for($dex=$weekday; $dex < 7; $dex++)
$h .= '<td>&nbsp;</td>'; $h .= '</tr></table>'; return $h;}

You may use this function provided that you leave the copyright notice intact.

To get the calendar looking reasonable we need to style the table to make it look like a calendar. The following is the CSS I used to style my example calendar which you can see at the bottom of this page.

.cal {margin: 0 auto; border:1pt solid #ccc; width:490px;
border-collapse:collapse;}
.cal th {width:70px; height:22px; vertical-align: top;background-color:#ccc;}
.cal td {width:70px; height:70px; text-align:right; vertical-align:top; border:1pt solid #ccc;}

There are a couple of different ways you can use this calendar function. The first way is to dynamically generate a calendar within the web page itself. To do this we simply echo the value returned from the function call as part of our page content. For example the sample calendar at the bottom of the page simply calls the function as follows:

echo calendar(date('n'), date('Y'));

This generates a calendar for the current month and year (and since the parameters are being returned from date functions we do not have a need to validate the values since they must always be valid). You can of course generate a calendar for any month of any year by passing the appropriate month and year values after validating them.

echo calendar($month, $year);

The other use for the function is in generating the HTML for the calendar in a way that can be made available for people to copy to use in web pages elsewhere. That just requires that the calendar function call be wrapped inside two other standard PHP function calls so as to display the HTML code for the calendar rather than displaying the calendar itself.

echo nl2br( htmlentities( calendar($month, $year) ));

Here's what the in page calendar looks like for the current month.

September 2016
SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
252627282930 

And here's what the HTML generated by the function looks like that could be copied to create basis for static calendar pages where you can manually add other information into your calendar. (note that as I am displaying it in a limited width those lines not starting with a <tr> tag have actually wrapped from the preceding line - not that the wrapping would affect the use of the code in any way).

<table border="0" cellspacing="0" cellpadding="0" class="cal"><tr><th colspan="7">September&nbsp;2016</th></tr>
<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td></tr>
<tr><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td></tr>
<tr><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td></tr>
<tr><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>&nbsp;</td></tr></table>