PHP生成月历代码

yipeiwu_com5年前PHP代码库
<?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>");
    }
}
?>

相关文章

应用开发中涉及到的css和php笔记分享

关于应用中遇到的一些问题及解决办法,做一些记录,以备后用。本人新手,技术还不熟练,笔记比较幼稚。嘲笑请轻笑。   1、关于层的绝对定位。   有两个div层,其中main类div是整体的...

php的SimpleXML方法读写XML接口文件实例解析

在php5中读写xml文档是非常方便的,可以直接使用php的SimpleXML方法来快速解析与生成xml格式的文件,下面举例说明: 创建一个SimpleXML对象有三种方法: 1.使用n...

与文件上传有关的php配置参数总结

搞个了图片上传,死活不好使,后来发现是php参数配置的问题。 下面总结下与文件上传有关的php参数,备忘之~ 所有这些参数都在php.ini中设置。1.file_uploads 设为On...

PHP实现动态创建XML文档的方法

PHP实现动态创建XML文档的方法

本文实例讲述了PHP实现动态创建XML文档的方法。分享给大家供大家参考,具体如下: 一. 代码 conn.php <?php $id=mysql_connect("lo...

spl_autoload_register与autoload的区别详解

spl_autoload_register(PHP 5 >= 5.1.2)spl_autoload_register — 注册__autoload()函数说明bool spl_au...