欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP生成月历代码

程序员文章站 2022-04-22 20:27:06
<?php
/*  
function written by nelson neoh @3/2004.  
for those who wants to utilize this code, please do not remove this remark.  
if you have done any enhancement to this code, please post the copy at http://www.dev-club.com php board.  thank you.

function usage: calendar(month,year)
*/

function calendar($mm,$yyyy){
    if($mm=="") $mm = date("m");
    if($yyyy=="") $yyyy = date("y");
    if(checkdate($mm,1,$yyyy)){
        $stringdate = strftime("%d %b %y",mktime (0,0,0,$mm,1,$yyyy));
        $days = strftime("%d",mktime (0,0,0,$mm+1,0,$yyyy));
        $firstday = strftime("%w",mktime (0,0,0,$mm,1,$yyyy));
        $lastday = strftime("%w",mktime (0,0,0,$mm,$days,$yyyy));
        $printdays = $days;
        $premonth = strftime("%m",mktime (0,0,0,$mm-1,1,$yyyy));
        $preyear = strftime("%y",mktime (0,0,0,$mm-1,1,$yyyy));
        $nextmonth = strftime("%m",mktime (0,0,0,$mm+1,1,$yyyy));
        $nextyear = strftime("%y",mktime (0,0,0,$mm+1,1,$yyyy));
        print("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">");
        print("<tr><th valign=\"top\"><a href=\"".$_server['php_self']."?nb=".$_get["nb"]."&mm=".$premonth."&yy=".$preyear."\">p</a></th>");
        print("<th colspan=\"5\" valign=\"top\">".strftime("%b %y",mktime (0,0,0,$mm,1,$yyyy))."</th>");
        print("<th valign=\"top\"><a href=\"".$_server['php_self']."?nb=".$_get["nb"]."&mm=".$nextmonth."&yy=".$nextyear."\">n</a></th></tr>");
        print("<tr style=\"font-family: verdana; font-size:x-small\">");
        print("<th>sun</th><th>mon</th><th>tue</th><th>wed</th><th>thu</th><th>fri</th><th>sat</th></tr>");

        $currentdays = 1;
        for($a=1;$a<=5;$a++){
            print("<tr align=\"left\" valign=\"top\" style=\"font-family: verdana; font-size:x-small\">");
            $diffdays = $firstday-$lastday;
            if($firstday>$lastday && $currentdays ==1 && ($diffdays<>1)){
                for($x=$lastday;$x>=0;$x--){
                    $printdays = $days-$x;
                    print("<td>$printdays</td>");
                }
                for($z=1;$z<$firstday-$lastday;$z++){
                    print("<td> </td>");
                }
                for($y=$firstday;$y<7;$y++){
                    print("<td>$currentdays</td>");
                    $currentdays++;
                }
            } elseif($firstday!=0 && $currentdays==1){
                for($z=1;$z<=$firstday;$z++){
                    print("<td> </td>");
                }
                for($y=$firstday;$y<7;$y++){
                    print("<td>$currentdays</td>");
                    $currentdays++;
                }
            } else {
                for($u=1;$u<=7 && $currentdays<=$days;$u++){
                    print("<td>$currentdays</td>");
                    $currentdays++;
                }
            }
            print("</tr>");
        }
        print("</table>");
    }
}
?>