PHP自动生成月历代码


Posted in PHP onOctober 09, 2006

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

 

PHP 相关文章推荐
7个超级实用的PHP代码片段
Jul 11 PHP
全新的PDO数据库操作类php版(仅适用Mysql)
Jul 22 PHP
深入Memcache的Session数据的多服务器共享详解
Jun 13 PHP
解决File size limit exceeded 错误的方法
Jun 14 PHP
php递归使用示例(php递归函数)
Feb 14 PHP
php利用scws实现mysql全文搜索功能的方法
Dec 25 PHP
smarty模板引擎中自定义函数的方法
Jan 22 PHP
PHP动态生成指定大小随机图片的方法
Mar 25 PHP
PHP实现二维数组根据key进行排序的方法
Dec 30 PHP
laravel通过创建自定义artisan make命令来新建类文件详解
Aug 17 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
Sep 13 PHP
详解PHP 二维数组排序保持键名不变
Mar 06 PHP
十天学会php(3)
Oct 09 #PHP
十天学会php(1)
Oct 09 #PHP
十天学会php(2)
Oct 09 #PHP
论坛头像随机变换代码
Oct 09 #PHP
PHP中路径问题的解决方案
Oct 09 #PHP
新浪新闻小偷
Oct 09 #PHP
如何使用PHP获取网络上文件
Oct 09 #PHP
You might like
php 数组动态添加实现代码(最土团购系统的价格排序)
2011/12/30 PHP
win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
2014/08/15 PHP
php简单实现数组分页的方法
2016/04/30 PHP
PHP QRCODE生成彩色二维码的方法
2016/05/19 PHP
php英文单词统计器
2016/06/23 PHP
PHP实现的方程求解示例分析
2016/11/11 PHP
用js解决数字不能换行问题
2010/08/10 Javascript
常见JS效果之图片减速度滚动实现代码
2011/12/08 Javascript
js 判断上传文件大小及格式代码
2013/11/13 Javascript
jQuery使用empty()方法删除元素及其所有子元素的方法
2015/03/26 Javascript
JavaScript实现数组在指定位置插入若干元素的方法
2015/04/06 Javascript
javascript实现很浪漫的气泡冒出特效
2020/09/05 Javascript
详解js中构造流程图的核心技术JsPlumb(2)
2015/12/08 Javascript
详解AngularJS中的http拦截
2016/02/09 Javascript
AngularJS中使用three.js的实例详解
2017/07/21 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
angularJS1 url中携带参数的获取方法
2018/10/09 Javascript
微信小程序在ios下Echarts图表不能滑动的问题解决
2019/07/10 Javascript
JS实现贪吃蛇游戏
2019/11/15 Javascript
Element MessageBox弹框的具体使用
2020/07/27 Javascript
vue组件是如何解析及渲染的?
2021/01/13 Vue.js
JS实现点击掉落特效
2021/01/29 Javascript
使用Python判断IP地址合法性的方法实例
2014/03/13 Python
Python缩进和冒号详解
2016/06/01 Python
基于python plotly交互式图表大全
2019/12/07 Python
PyTorch里面的torch.nn.Parameter()详解
2020/01/03 Python
css3 按钮样式简单可扩展创建
2013/03/18 HTML / CSS
HTML5 canvas基本绘图之填充样式实现
2016/06/27 HTML / CSS
HTML5 LocalStorage 本地存储详细概括(多图)
2017/08/18 HTML / CSS
澳大利亚领先的在线机械五金、园艺和存储专家:Edisons
2018/03/24 全球购物
电子狗项圈:eDog Australia
2019/12/04 全球购物
线程同步的方法
2016/11/23 面试题
党员检讨书范文
2014/12/27 职场文书
试用期工作表现自我评价
2015/03/06 职场文书
Python基于百度API识别并提取图片中文字
2021/06/27 Python
MySQL中CURRENT_TIMESTAMP的使用方式
2021/11/27 MySQL