PHP自动生成月历代码

所属分类: 网络编程 / PHP编程 阅读数: 1872
收藏 0 赞 0 分享

<?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>&nbsp;</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>&nbsp;</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>");
    }
}
?>

 

更多精彩内容其他人还在看

php实现指定字符串中查找子字符串的方法

这篇文章主要介绍了php实现指定字符串中查找子字符串的方法,涉及php中strpos()函数查找字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php去除字符串中空字符的常用方法小结

这篇文章主要介绍了php去除字符串中空字符的常用方法,实例分析了php中的trim()、ltrim()、rtrim()及chop()等函数的使用技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php通过rmdir删除目录的简单用法

这篇文章主要介绍了php通过rmdir删除目录的简单用法,实例分析了rmdir与mkdir函数的功能及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php通过function_exists检测函数是否存在的方法

这篇文章主要介绍了php通过function_exists检测函数是否存在的方法,实例分析了php使用function_exists检测函数是否存在及调用的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php中ob_flush函数和flush函数用法分析

这篇文章主要介绍了php中ob_flush函数和flush函数用法,实例分析了ob_flush函数和flush函数的功能及相关的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php随机生成数字字母组合的方法

这篇文章主要介绍了php随机生成数字字母组合的方法,实例分析了php生成随机数及随机字母的相关技巧与用法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php防止sql注入简单分析

这篇文章主要介绍了php防止sql注入的方法,简单分析了通过stripslashes及mysql_real_escape_string函数进行字符转移处理的技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

php制作文本式留言板

本文给大家分享的是使用php结合文本文件制作的留言板的代码,非常简单,实现了常用的功能,推荐给大家,有需要的小伙伴参考下吧。
收藏 0 赞 0 分享

php通过ksort()函数给关联数组按照键排序的方法

这篇文章主要介绍了php通过ksort()函数给关联数组按照键排序的方法,实例分析了php中ksort()函数的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP 实现类似js中alert() 提示框

这篇文章主要介绍了PHP 实现类似js中alert() 提示框功能,非常的实用,这里推荐给大家,有需要的小伙伴来参考下,希望大家能喜欢。
收藏 0 赞 0 分享
查看更多