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 相关文章推荐
用缓存实现静态页面的测试
Dec 06 PHP
利用PHP和AJAX创建RSS聚合器的代码
Mar 13 PHP
PHP 代码规范小结
Mar 08 PHP
php数据结构与算法(PHP描述) 查找与二分法查找
Jun 21 PHP
php中is_null,empty,isset,unset 的区别详细介绍
Apr 28 PHP
深入apache host的配置详解
Jun 09 PHP
控制PHP的输出:缓存并压缩动态页面
Jun 11 PHP
php教程之魔术方法的使用示例(php魔术函数)
Feb 12 PHP
如何利用http协议发布博客园博文评论
Aug 03 PHP
PHP常见数组函数用法小结
Mar 21 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
详解no input file specified 三种解决方法
Nov 29 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中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
2017/09/17 PHP
基于PHP实现的多元线性回归模拟曲线算法
2018/01/30 PHP
Smarty模板类内部原理实例分析
2019/07/03 PHP
在TP5数据库中四个字段实现无限分类的示例
2019/10/18 PHP
javascript中使用css需要注意的地方小结
2010/09/01 Javascript
javascript 文本框水印/占位符(watermark/placeholder)实现方法
2012/01/15 Javascript
使用jQuery清空file文件域的解决方案
2013/04/12 Javascript
动态改变div的z-index属性的简单实例
2013/08/08 Javascript
JS实现的几个常用算法
2016/11/12 Javascript
客户端(vue框架)与服务器(koa框架)通信及服务器跨域配置详解
2017/08/26 Javascript
一个简易时钟效果js实现代码
2020/03/25 Javascript
详解ES6语法之可迭代协议和迭代器协议
2018/01/13 Javascript
如何编写一个d.ts文件的步骤详解
2018/04/13 Javascript
chosen实现省市区三级联动
2018/08/16 Javascript
Javascript读写cookie的实例源码
2019/03/16 Javascript
ionic4+angular7+cordova上传图片功能的实例代码
2019/06/19 Javascript
JS变量提升及函数提升实例解析
2020/09/03 Javascript
[04:23]DOTA2上海特锦赛小组赛第一日 TOP10精彩集锦
2016/02/27 DOTA
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
selenium+python截图不成功的解决方法
2019/01/30 Python
在Python中过滤Windows文件名中的非法字符方法
2019/06/10 Python
python安装pil库方法及代码
2019/06/25 Python
python开启debug模式的方法
2019/06/27 Python
python读取指定字节长度的文本方法
2019/08/27 Python
PyCharm 2020.2 安装详细教程
2020/09/25 Python
PyTorch中clone()、detach()及相关扩展详解
2020/12/09 Python
LTD Commodities:礼品,独特发现,家居装饰,家用器皿
2017/08/11 全球购物
Joules官网:女士、男士和儿童服装和鞋类
2018/10/23 全球购物
五好党支部事迹材料
2014/02/06 职场文书
《尊严》教学反思
2014/02/11 职场文书
刑事案件上诉状
2015/05/23 职场文书
党内外群众意见范文
2015/06/02 职场文书
小组口号霸气押韵
2015/12/24 职场文书
我的中国梦心得体会范文
2016/01/05 职场文书
MySQL的安装与配置详细教程
2021/06/26 MySQL
python开发人人对战的五子棋小游戏
2022/05/02 Python