note 20347 added to function.getdate
| From: | sigve at NEXT_IN_ALPHABETdkdu dot mp | Date: | Sun, 31 Mar 2002 17:01:19 +0000 |
| Subject: | note 20347 added to function.getdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-28429@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\"> </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\"> </td>";
}
echo "</table>";
}
--
http://www.php.net/manual/en/function.getdate.php
http://master.php.net/manage/user-notes.php?action=edit+20347
http://master.php.net/manage/user-notes.php?action=delete+20347
http://master.php.net/manage/user-notes.php?action=reject+20347