3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /* draws a calendar */ function draw_calendar($month,$year){ /* draw table */ $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">'; /* table headings */ $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>'; /* days and weeks vars now ... */ $running_day = date('w',mktime(0,0,0,$month,1,$year)); $days_in_month = date('t',mktime(0,0,0,$month,1,$year)); $days_in_this_week = 1; $day_counter = 0; $dates_array = array(); /* row for week one */ $calendar.= '<tr class="calendar-row">'; /* print "blank" days until the first of the current week */ for($x = 0; $x < $running_day; $x++): $calendar.= '<td class="calendar-day-np"> </td>'; $days_in_this_week++; endfor; /* keep going with days.... */ for($list_day = 1; $list_day <= $days_in_month; $list_day++): $calendar.= '<td class="calendar-day">'; /* add in the day number */ $calendar.= '<div class="day-number">'.$list_day.'</div>'; /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !! IF MATCHES FOUND, PRINT THEM !! **/ $calendar.= str_repeat('<p> </p>',2); $calendar.= '</td>'; if($running_day == 6): $calendar.= '</tr>'; if(($day_counter+1) != $days_in_month): $calendar.= '<tr class="calendar-row">'; endif; $running_day = -1; $days_in_this_week = 0; endif; $days_in_this_week++; $running_day++; $day_counter++; endfor; /* finish the rest of the days in the week */ if($days_in_this_week < 8): for($x = 1; $x <= (8 - $days_in_this_week); $x++): $calendar.= '<td class="calendar-day-np"> </td>'; endfor; endif; /* final row */ $calendar.= '</tr>'; /* end the table */ $calendar.= '</table>'; /* all done, return result */ return $calendar; } /* sample usages */ echo '<h2>July 2009</h2>'; echo draw_calendar(7,2009); echo '<h2>August 2009</h2>'; echo draw_calendar(8,2009); ?>
Output for 4.3.0 - 7.1.0
<h2>July 2009</h2><table cellpadding="0" cellspacing="0" class="calendar"><tr class="calendar-row"><td class="calendar-day-head">Sunday</td><td class="calendar-day-head">Monday</td><td class="calendar-day-head">Tuesday</td><td class="calendar-day-head">Wednesday</td><td class="calendar-day-head">Thursday</td><td class="calendar-day-head">Friday</td><td class="calendar-day-head">Saturday</td></tr><tr class="calendar-row"><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day"><div class="day-number">1</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">2</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">3</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">4</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">5</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">6</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">7</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">8</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">9</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">10</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">11</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">12</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">13</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">14</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">15</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">16</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">17</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">18</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">19</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">20</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">21</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">22</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">23</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">24</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">25</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">26</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">27</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">28</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">29</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">30</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">31</div><p> </p><p> </p></td><td class="calendar-day-np"> </td></tr></table><h2>August 2009</h2><table cellpadding="0" cellspacing="0" class="calendar"><tr class="calendar-row"><td class="calendar-day-head">Sunday</td><td class="calendar-day-head">Monday</td><td class="calendar-day-head">Tuesday</td><td class="calendar-day-head">Wednesday</td><td class="calendar-day-head">Thursday</td><td class="calendar-day-head">Friday</td><td class="calendar-day-head">Saturday</td></tr><tr class="calendar-row"><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day"><div class="day-number">1</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">2</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">3</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">4</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">5</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">6</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">7</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">8</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">9</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">10</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">11</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">12</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">13</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">14</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">15</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">16</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">17</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">18</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">19</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">20</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">21</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">22</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">23</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">24</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">25</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">26</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">27</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">28</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">29</div><p> </p><p> </p></td></tr><tr class="calendar-row"><td class="calendar-day"><div class="day-number">30</div><p> </p><p> </p></td><td class="calendar-day"><div class="day-number">31</div><p> </p><p> </p></td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td><td class="calendar-day-np"> </td></tr></table>