PHP Simple Calendar…

I have been looking for a proper, simple and well formed PHP calendar. I eventually found a promising looking script on emanueleferonato.com however it was not well formed (PHP or Output HTML)

I have tweaked the script to be correctly formed 😀 You can output any month of any year (within PHP restrictions) and you can start you week on any day of the week. i.e. Americans start on Saturday where as the English start on Monday.

The HTML Output is well formed, as shown below:

<table border="1" width="300px">
<tbody>
<tr>
<td colspan="7">November 2010</td>
</tr>
<tr>
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
<tr>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
<td>26</td>
<td>27</td>
</tr>
<tr>
<td>28</td>
<td>29</td>
<td>30</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

As well as the PHP itself:

<pre><!--?
//Cal Function
function calendar($year, $month, $day_offset = 0){
 $days = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
 $months = array("January","February","March","April","May","June","July","August","September","October","November","December");

 $day_offset = $day_offset % 7;
 $start_day = gmmktime(0, 0, 0, $month, 1, $year);
 $start_day_number = date("w", $start_day);
 $days_in_month = date("t", $start_day);
 $final_html .= '
<table border="1" width="300px"-->' . "n" . ' ' . "n" . '   ' . $months[$month-1] . ' ' . $year . '' . "n" . ' ' . "n";

 for($x=0; $x<=6; $x++){
 $final_html .= '   ' . $days[($x+$day_offset)%7] . '' . "n";
 }

 $final_html .= ' ' . "n";
 $blank_days = $start_day_number - $day_offset;

 if($blank_days < 0) { $blank_days = 7-abs($blank_days); }

 for($x=0; $x < $blank_days; $x++) {
 $final_html .= '    ' . "n";
 }

 for($x=1; $x <= $days_in_month; $x++){
 if(($x+$blank_days-1) % 7 == 0){
 $final_html .= ' ' . "n" . ' ' . "n";
 }
 $final_html .= '   ' . $x . '' . "n";
 }

 while((($days_in_month+$blank_days) % 7) != 0) {
 $final_html .= '    ' . "n";
 $days_in_month++;
 }

 $final_html .= ' ' . "n" . '';

 return($final_html);
}
//End: Cal Function

// calendar ( YEAR , MONTH , MONTH-STARTS-ON-WHAT-DAY )
echo calendar(2010, 11, 0);
?>

Simply place the function called calendar (defined by the PHP comments) at the top of your page and call the function where ever you wish. To call the function :

<!--? echo calendar(2010, 11, 0); ?--><?
echo calendar(2010, 11, 0);
?>

Replace “2010” with the year you wish, along with “11” as the month you require and you can choose which day of the week your week starts on by replacing the “0” with the corresponding answer (0 = Sun, 1 = Mon, 2 = Tue etc.)

PHP Simple Calendar…

One thought on “PHP Simple Calendar…

Leave a Reply

Your email address will not be published. Required fields are marked *