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+ACCESS 文章管理程序代码
Jun 21 PHP
PHP 验证码的实现代码
Jul 17 PHP
用PHP书写安全的脚本代码
Feb 05 PHP
解析php利用正则表达式解决采集内容排版的问题
Jun 20 PHP
请离开include_once和require_once
Jul 18 PHP
PHP创建桌面快捷方式的实例代码
Feb 17 PHP
利用php做服务器和web前端的界面进行交互
Oct 31 PHP
PHP微信开发之微信录音临时转永久存储
Jan 26 PHP
php写入文件不覆盖的实例讲解
Sep 17 PHP
Laravel 微信小程序后端搭建步骤详解
Nov 26 PHP
php多进程中的阻塞与非阻塞操作实例分析
Mar 04 PHP
浅谈PHP7中的一些小技巧
May 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中的escape函数
2013/06/29 PHP
几道坑人的PHP面试题 试试看看你会不会也中招
2014/08/19 PHP
thinkphp3.2点击刷新生成验证码
2016/02/16 PHP
CI框架表单验证实例详解
2016/11/21 PHP
Thinkphp5行为使用方法汇总
2017/12/21 PHP
PHP实现获取ip地址的5种方法,以及插入用户登录日志操作示例
2019/02/28 PHP
jquery中dom操作和事件的实例学习 下拉框应用
2011/12/01 Javascript
JavaScript数组随机排列实现随机洗牌功能
2015/03/19 Javascript
JavaScript实现找质数代码分享
2015/03/24 Javascript
Jsonp post 跨域方案
2015/07/06 Javascript
详解页面滚动值scrollTop在FireFox与Chrome浏览器间的兼容问题
2015/12/03 Javascript
Javascript中的Prototype到底是什么
2016/02/16 Javascript
BootStrap 智能表单实战系列(五) 表单依赖插件处理
2016/06/13 Javascript
利用Query+bootstrap和js两种方式实现日期选择器
2017/01/10 Javascript
Angular4如何自定义首屏的加载动画详解
2017/07/26 Javascript
原生JavaScript来实现对dom元素class的操作方法(推荐)
2017/08/16 Javascript
Node.js中使用mongoose操作mongodb数据库的方法
2017/09/12 Javascript
微信小程序图片选择区域裁剪实现方法
2017/12/02 Javascript
微信小程序模板(template)使用详解
2018/01/31 Javascript
Node.js静态服务器的实现方法
2018/02/28 Javascript
微信小程序自定义tabBar的踩坑实践记录
2020/11/06 Javascript
Python警察与小偷的实现之一客户端与服务端通信实例
2014/10/09 Python
浅谈Python中的数据类型
2015/05/05 Python
Python实现高效求解素数代码实例
2015/06/30 Python
python八皇后问题的解决方法
2018/09/27 Python
解决Python对齐文本字符串问题
2019/08/28 Python
pygame实现五子棋游戏
2019/10/29 Python
浅谈CSS3 动画卡顿解决方案
2019/01/02 HTML / CSS
详解移动端HTML5页面端去掉input输入框的白色背景和边框(兼容Android和ios)
2016/12/15 HTML / CSS
html2canvas生成的图片偏移不完整的解决方法
2020/05/19 HTML / CSS
你所知道的集合类都有哪些?主要方法?
2012/12/31 面试题
.NET现在共支持多少种语言
2014/02/26 面试题
2014学习全国两会精神心得体会2000字
2014/03/11 职场文书
员工生日活动方案
2014/08/24 职场文书
2016春季运动会开幕词
2016/03/04 职场文书
MySQL 覆盖索引的优点
2021/05/19 MySQL