php Calender(日历)代码分享


Posted in PHP onJanuary 03, 2014

代码如下:

<?php
/**
 * 
 * 我的日历
 * date_default_timezone_set date mktime
 * @param int $year
 * @param int $month
 * @param string $timezone
 * @author fc_lamp
 */
function myCalender($year = '', $month = '', $timezone = 'Asia/Shanghai')
{    date_default_timezone_set ( $timezone );
    $year = abs ( intval ( $year ) );
    $month = abs ( intval ( $month ) );
    //是否是32位机
    if (is32())
    {
        if ($year < 1970 or $year >= 2038)
        {
            $year = date ( 'Y' );
        }
    } else
    {
        if ($year <= 0)
        {
            $year = date ( 'Y' );
        }
    }
    if ($month <= 0 or $month > 12)
    {
        $month = date ( 'm' );
    }
    //上一年
    $pretYear = $year - 1;
    //上一月
    $mpYear = $year;
    $preMonth = $month - 1;
    if ($preMonth <= 0)
    {
        $preMonth = 1;
        $mpYear = $pretYear;
    }
    //下一年
    $nextYear = $year + 1;
    //下一月
    $mnYear = $year;
    $nextMonth = $month + 1;
    if ($nextMonth > 12)
    {
        $nextMonth = 1;
        $mnYear = $nextYear;
    }
    //日历头
    $html = <<<HTML
<table width="500" border="1">
  <tr align="center">
    <td><a href="?y=$pretYear">上一年</a></td>
    <td><a href="?y=$mpYear&m=$preMonth">上一月</a></td>
     <td><a href="?">回到今天</a></td>
    <td><a href="?y=$mnYear&m=$nextMonth">下一月</a></td>
    <td><a href="?y=$nextYear">下一年</a></td>
  </tr>
  <tr align="center">
    <td colspan="5">{$year}年{$month}月</td>
  </tr>
  <tr>
      <td colspan="5">
        <table width="100%" border="1">
            <tr align="center">
                <td style="background-color:#DAF0DD;">星期一</td>
                <td style="background-color:#DAF0DD;">星期二</td>
                <td style="background-color:#DAF0DD;">星期三</td>
                <td style="background-color:#DAF0DD;">星期四</td>
                <td style="background-color:#DAF0DD;">星期五</td>
                <td style="background-color:#F60;color:#fff;font-weight: bold;">星期六</td>
                <td style="background-color:#F60;color:#fff;font-weight: bold;">星期天</td>
            </tr>
HTML;
    $currentDay = date ( 'Y-m-j' );
    //当月最后一天
    $lastday = date ( 'j', mktime ( 0, 0, 0, $nextMonth, 0, $year ) );
    //循环输出天数
    $day = 1;
    $line = '';
    while ( $day <= $lastday )
    {
        $cday = $year . '-' . $month . '-' . $day;
        //当前星期几
        $nowWeek = date ( 'N', mktime ( 0, 0, 0, $month, $day, $year ) );
        if ($day == 1)
        {
            $line = '<tr align="center">';
            $line .= str_repeat ( '<td> </td>', $nowWeek - 1 );
        }
        if ($cday == $currentDay)
        {
            $style = 'style="color:red;"';
        } else
        {
            $style = '';
        }
        $line .= "<td $style>$day</td>";
        //一周结束
        if ($nowWeek == 7)
        {
            $line .= '</tr>';
            $html .= $line;
            $line = '<tr align="center">';
        }
        //全月结束
        if ($day == $lastday)
        {
            if ($nowWeek != 7)
            {
                $line .= str_repeat ( '<td> </td>', 7 - $nowWeek );
            }
            $line .= '</tr>';
            $html .= $line;
            break;
        }
        $day ++;
    }
    $html .= <<<HTML
        </table>    
    </td>
  </tr>
</table>
HTML;
    return $html;
}

/**
 * 
 * 检测是否是32位机
 * @author fc_lamp
 * @blog: fc-lamp.blog.163.com
 */
function is32()
{
    $is32 = False;
    if (strtotime ( '2039-10-10' ) === False)
    {
        $is32 = True;
    }
    return $is32;
}
PHP 相关文章推荐
PHP的面向对象编程
Oct 09 PHP
基于PHP与XML的PDF文档生成技术
Oct 09 PHP
Mysql中limit的用法方法详解与注意事项
Apr 19 PHP
php日期转时间戳,指定日期转换成时间戳
Jul 17 PHP
基于PHP编程注意事项的小结
Apr 27 PHP
解析php中eclipse 用空格替换 tab键
Jun 24 PHP
php float不四舍五入截取浮点型字符串方法总结
Oct 28 PHP
memcache一致性hash的php实现方法
Mar 05 PHP
PHP时间和日期函数详解
May 08 PHP
[原创]php正则删除img标签的方法示例
May 27 PHP
PHP文件系统管理(实例讲解)
Sep 19 PHP
PHP实现的mysql读写分离操作示例
May 22 PHP
深入解读php中关于抽象(abstract)类和抽象方法的问题分析
Jan 03 #PHP
PHP运行SVN命令显示某用户的文件更新记录的代码
Jan 03 #PHP
PHP抓屏函数实现屏幕快照代码分享
Jan 02 #PHP
php curl模拟post提交数据示例
Dec 31 #PHP
codeigniter使用技巧批量插入数据实例方法分享
Dec 31 #PHP
PHP字符串的连接的简单实例
Dec 30 #PHP
php实现执行某一操作时弹出确认、取消对话框
Dec 30 #PHP
You might like
PHP 和 MySQL 基础教程(二)
2006/10/09 PHP
很让人受教的 提高php代码质量36计
2012/09/05 PHP
php权重计算方法代码分享
2014/01/09 PHP
php解压缩zip和rar压缩包文件的方法
2019/07/10 PHP
javascript+xml技术实现分页浏览
2008/07/27 Javascript
asp.net下利用js实现返回上一页的实现方法小集
2009/11/24 Javascript
Javascript类定义语法,私有成员、受保护成员、静态成员等介绍
2011/12/08 Javascript
Javascript创建自定义对象 创建Object实例添加属性和方法
2012/06/04 Javascript
一张表格告诉你windows.onload()与$(document).ready()的区别
2014/05/16 Javascript
JavaScript实现的GBK、UTF8字符串实际长度计算函数
2014/08/27 Javascript
node.js中的fs.createReadStream方法使用说明
2014/12/17 Javascript
jQuery中dom元素上绑定的事件详解
2015/04/24 Javascript
js实现砖头在页面拖拉效果
2020/11/20 Javascript
js格式化时间的简单实例
2016/11/27 Javascript
AngularJS封装指令方法详解
2016/12/12 Javascript
jquery 正整数数字校验正则表达式
2017/01/10 Javascript
vue使用Proxy实现双向绑定的方法示例
2019/03/20 Javascript
js实现飞机大战游戏
2020/08/26 Javascript
python爬取NUS-WIDE数据库图片
2016/10/05 Python
Python实现将数据库一键导出为Excel表格的实例
2016/12/30 Python
Python if语句知识点用法总结
2018/06/10 Python
Python生成指定数量的优惠码实操内容
2019/06/18 Python
python查找重复图片并删除(图片去重)
2019/07/16 Python
浅谈python多线程和多线程变量共享问题介绍
2020/04/17 Python
Python中猜拳游戏与猜筛子游戏的实现方法
2020/09/04 Python
Numpy中np.random.rand()和np.random.randn() 用法和区别详解
2020/10/23 Python
python UDF 实现对csv批量md5加密操作
2021/01/01 Python
Pure Collection美国官网:来自英国羊绒专家的奢华羊绒
2017/11/19 全球购物
美国时尚大码女装购物网站:Avenue
2019/05/24 全球购物
毕业生求职自荐信怎么写
2014/01/08 职场文书
2014端午节活动策划方案
2014/01/27 职场文书
《我的伯父鲁迅先生》教学反思
2014/02/12 职场文书
车队司机自我鉴定
2014/03/02 职场文书
网络营销计划书
2015/01/17 职场文书
浅谈golang 中time.After释放的问题
2021/05/05 Golang
vue实现简易音乐播放器
2022/08/14 Vue.js