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 相关文章推荐
使用MaxMind 根据IP地址对访问者定位
Oct 09 PHP
php创建基本身份认证站点的方法详解
Jun 08 PHP
PHP写的加密函数,支持私人密钥(详细介绍)
Jun 09 PHP
解析php如何将日志写进syslog
Jun 28 PHP
php中mysql操作buffer用法详解
Mar 19 PHP
PHP检测用户语言的方法
Jun 15 PHP
thinkPHP导出csv文件及用表格输出excel的方法
Dec 30 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
Zend Framework实现将session存储在memcache中的方法
Mar 22 PHP
详解EventDispatcher事件分发组件
Dec 25 PHP
yii2中dropDownList实现二级和三级联动写法
Apr 26 PHP
PHP面向对象程序设计继承用法简单示例
Dec 28 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和网站发送到邮箱实例代码
2013/11/28 PHP
PHP base64编码后解码乱码的解决办法
2014/06/19 PHP
destoon整合ucenter后注册页面不跳转的解决方法
2014/06/21 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
2014/08/25 PHP
PHP实现微信网页授权开发教程
2016/01/19 PHP
微信支付PHP SDK ―― 公众号支付代码详解
2016/09/13 PHP
Laravel等框架模型关联的可用性浅析
2019/12/15 PHP
PHP设计模式之迭代器模式Iterator实例分析【对象行为型】
2020/04/26 PHP
javascript之典型高阶函数应用介绍二
2013/01/10 Javascript
JS获取地址栏参数的小例子
2013/08/23 Javascript
使用Node.js实现一个简单的FastCGI服务器实例
2014/06/09 Javascript
在Linux上用forever实现Node.js项目自启动
2014/07/09 Javascript
JS判断网页广告是否被浏览器拦截过滤的代码
2015/04/05 Javascript
jQuery.uploadify文件上传组件实例讲解
2016/09/23 Javascript
前端js实现文件的断点续传 后端PHP文件接收
2016/10/14 Javascript
Bootstrap模态框(Modal)实现过渡效果
2017/03/17 Javascript
基于react框架使用的一些细节要点的思考
2017/05/31 Javascript
JS简单添加元素新节点的方法示例
2018/02/10 Javascript
JavaScript中将值转换为字符串的五种方法总结
2019/06/06 Javascript
JS中this的4种绑定规则详解
2020/02/04 Javascript
JS内置对象和Math对象知识点详解
2020/04/03 Javascript
JS数组索引检测中的数据类型问题详解
2021/01/11 Javascript
Python的socket模块源码中的一些实现要点分析
2016/06/06 Python
使用NumPy和pandas对CSV文件进行写操作的实例
2018/06/14 Python
python命令行工具Click快速掌握
2019/07/04 Python
Pycharm操作Git及GitHub的步骤详解
2020/10/27 Python
ktv周年庆活动方案
2014/08/18 职场文书
离婚协议书怎么写(范本参考)
2014/09/30 职场文书
公司年会开场白
2015/06/01 职场文书
社区挂职锻炼个人工作总结
2015/10/23 职场文书
高中班主任培训心得体会
2016/01/07 职场文书
2016年小学教师师德承诺书
2016/03/25 职场文书
机关单位2016年创先争优活动总结
2016/04/05 职场文书
html+css实现赛博朋克风格按钮
2021/05/26 HTML / CSS
嵌入式Redis服务器在Spring Boot测试中的使用教程
2021/07/21 Redis
vue实现Toast组件轻提示
2022/04/10 Vue.js