Mega Code Archive

 
Categories / Php / Date Time
 

Dynamic Calendar (easiest ever)

<style type="text/css"> <!-- td { font-size:12pt; line-height:14pt; font-family:Helvetica,Arial; } //--> </style> <? ## change to the previous month if ($command == "prev_month") { if (--$month == 0) { $month = 12; $year--; } } ## change to the next month else if ($command == "next_month") { if (++$month == 13) { $month = 1; $year++; } } ## if no month has been passed if ($month == "") { $year= date("Y"); $month= date("n"); $month_name = date("F",mktime(0,0,0,$month,1,$year)); } ## use current date if no month is passed else { $year= date("Y",mktime(0,0,0,$month,1,$year)); $month= date("n",mktime(0,0,0,$month,1,$year)); $month_name = date("F",mktime(0,0,0,$month,1,$year)); } $dow = date("w",mktime(0,0,0,$month,1,$year)); //which day 1 falls on in the week (0 = Sun) $dim = date("d",mktime (0,0,0,$month+1,0,$year)); //days in the current month ## modification to only print the number of weeks in a month $wim = ceil(($dim+$dow)/7); //weeks in month $ct=0; echo "<br> <table border='0' cellpadding='1' cellspacing='1' width='425' align='center'> <tr> <td align='center'><h1><a href='index.php? command=prev_month&month=$month&year=$year'><<</a> $month_name $year <a href='index.php?command=next_month&month=$month&year=$year'>>></a></h1></td> </tr> </table> <table border='1' cellpadding='1' cellspacing='1' width='425' align='center'> <tr> <td align='center'><b>Sun</td> <td align='center'><b>Mon</td> <td align='center'><b>Tue</td> <td align='center'><b>Wed</td> <td align='center'><b>Thu</td> <td align='center'><b>Fri</td> <td align='center'><b>Sat</td> </tr> "; ## print only the number of weeks needed for($row=1;$row<$wim+1;$row++) { echo "<tr height='60'>"; ## prints week (Sun to Sat) for($week=1;$week<8;$week++) { $ct++; $value=mktime(0,0,0,$month,$ct-$dow,$year); ## if $value is part of current month if (date("m",$value)==$month) { echo " <td align='center' class='body' width='85' valign='top'> <div align='right'><b>".date("j",$value)."</a></b></div><br> </td>"; } ## print previous and next month dates, but grayed out else { echo "<td align='center' class='body' width='85' valign='top' bgcolor='#CCCCCC'><div align='right'><b>".date("j",$value)."</a></b></div><br></td>"; } } echo "</tr>"; } echo "</table>"; ?>