PHP Calendar with wrong start date for august 2015 -
i have got above code about.com. works except august 2015. start date should saturday calendar shows monday instead. other months have checked far correct.
any hint?
<?php function calendar() { date_default_timezone_set('utc'); $date = time(); // removed orginally. if (isset($_request['emonth'])&& isset($_request['eyear'])){ $month = $_request['emonth']; $year = $_request['eyear']; } else { $month = date('m', $date); $year = date('y', $date); } $first_day = mktime(0,0,0,$month, 1, $year); echo date('d', $first_day); $title = date('f', $first_day); $day_of_week = date('d', $first_day); switch($day_of_week){ case "sun": $blank = 0; break; case "mon": $blank = 1; break; case "tue": $blank = 2; break; case "wed": $blank = 3; break; case "thu": $blank = 4; break; case "fri": $blank = 5; break; case "sar": $blank = 6; break; } $days_in_month = cal_days_in_month(0, $month, $year); echo '<table class="event"> <tr> <td colspan="7">'.$title.'-'.$year.'</td> </tr><tr> <th>sun</th><th>mon</th><th>tue</th><th>wed</th><th>thu</th><th>fri</th><th>sat</th> </tr>'; $day_count = 1; echo '<tr>'; while ($blank > 0) { echo '<td></td>'; $blank = $blank-1; $day_count++; } $day_num = 1; while ($day_num <= $days_in_month){ echo "<td>$day_num</td>"; $day_num++; $day_count++; if ($day_count > 7) { echo '</tr><tr>'; $day_count = 1; while ($day_count > 1 && $day_count <= 7) { echo '<td></td>'; $day_count++; } } } echo '</tr> </table>'; } calendar(); ?>
solved
there typo of sar instead of sat in switch case. since worked of months i've checked, overlooked it. works after correction.
thanks guys
you not defining $date
- although not necessary in example. date()
function default today if no second parameter passed in. (also isset
can take comma separated list of variables cleaner)
if (isset($_request['emonth'],$_request['eyear'])){ $month = $_request['emonth']; $year = $_request['eyear']; } else { $month = date('m'); $year = date('y'); }
also, mentioned in comments, have typo in switch statement prevent match on 'sat'.
Comments
Post a Comment