note 20347 rejected from function.getdate by victor

From: Date: Sat, 24 Aug 2002 16:16:31 +0000
Subject: note 20347 rejected from function.getdate by victor
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-35539@lists.php.net to get a copy of this message
This function, provided as it is used in one of my programs, produces a little table of a month. $y and $m are obviously the year and month you want to print, $href is where you want the dates to link and the others are details of which date is current (should not be linked anywhere). Layout of the headers is provided by CSS. Current day defaults to today. function printcal ($y, $m, $href, $currd=0, $curry=0, $currm=0) { if ($currd==0) { $today=getdate(time()); $currd=$today["mday"]; } if ($curry==0) { $today=getdate(time()); $curry=$today["year"]; } if ($currm==0) { $today=getdate(time()); $currm=$today["mon"]; } //find weekday of $m 1st. $begin_month=mktime(0, 0, 0, $m, 1, $y); $begin_month_assoc=getdate($begin_month); $begin_month_wday=$begin_month_assoc["wday"]; $curr_month_name=$begin_month_assoc["month"]." ".$begin_month_assoc["year"]; //sunday should be last, not first $begin_month_wday--; if ($begin_month_wday < 0) $begin_month_wday=6; //find number of days in month $end_month=mktime(0,0,0,$m+1,1,$y) - 1; // 1 sec before 0:00:00 on the first of next month $end_month_assoc=getdate($end_month); $end_month_day=$end_month_assoc["mday"]; //now print it, in a table, starting with monday(0) and ending with sunday(6) ?> <table class="kalender" cellspacing=1 cellpadding=0> <tr> <th colspan=7><?=$curr_month_name?></th> </tr> <tr> <th>M</th> <th>T</th> <th>O</th> <th>T</th> <th>F</th> <th>L</th> <th>S</th> </tr> <? //change color for every td (nifty in 7-td tables) $color[0]="#BBBBBB"; $color[1]="#DDDDDD"; $currcolor=0; //first make the TDs up to the point of the 1st: $first=true; for ($x=0; $x < $begin_month_wday; $x++) { if ($first) { echo "\t<tr>\n"; $first=false; } if ($currcolor==1) $currcolor=0; else $currcolor=1; $ccolor=$color[ $currcolor ]; echo "\t\t<td bgcolor=\"$ccolor\">&nbsp;</td>\n"; } //maintaining $x as a <tr> advisor, we keep on going until we've written all dates for ($day=1; $day <= $end_month_day; $day++) { $x++; if ($x==1) { echo "\t<tr>\n"; } if ($currcolor==1) $currcolor=0; else $currcolor=1; $ccolor=$color[ $currcolor ]; if ($m==$currm AND $y==$curry AND $currd==$day) { echo "\t\t<td bgcolor=\"$ccolor\" align=\"center\">$day</td>\n"; } else { echo "\t\t<td bgcolor=\"$ccolor\" align=\"center\"><a href=\"$href?m=$m&y=$y&d=$day\">$day</a></td>\n"; } if ($x==7) { echo "\t</tr>\n"; $x=0; } } for (;$x<7 and $x > 0;$x++) { if ($currcolor==1) $currcolor=0; else $currcolor=1; $ccolor=$color[ $currcolor ]; echo "\t\t<td bgcolor=\"$ccolor\">&nbsp;</td>"; } echo "</table>"; }

« previous php.notes (#35539) next »