This one’s simpler based on php 5.3 and more calendar functions
14 fewer lines and it’s using timestamps for each grid square, so you can use the dates well for whatever other purposes.
function grid_cal($m){ $bag = ""; $m = strtotime("first day of this month",$m); $i = strtotime("-" . date("w",$m) . " days",$m); // starts on the previous sunday $bag .= "<table class=\"cal-table\"><tr class=\"cal-month\">\n<td colspan=\"7\">" .date("F",$m) ." ". date("Y",$m) . "</td>\n</tr>\n<tr class=\"cal-days\">"; for($j=0;$j<7;$j++){ $bag .= "\n<td>" . date("D",strtotime("+$j days",$i)) . "</td>"; } $bag .="\n</tr>"; while(!(date("m",$i)>date("m",$m) && date("w",$i)==0)){ if(date("w",$i) == 0) $bag .= "\n<tr>"; $bag .= (date("m",$i)==date("m",$m)) ? "\n<td>" . date("j",$i) . "</td>" : "<td class='emptygrid'></td>"; if(date("w",$i) == 6) $bag .= "\n</tr>"; $i = strtotime("+1 day",$i); } $bag .= "\n</table>"; return $bag; }
here’s how you’d use it…
print grid_cal(time());
Leave a Reply