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部分常见问题总结
Mar 27 PHP
php str_pad 函数使用详解
Jan 13 PHP
PHP中常用的输出函数总结
Sep 22 PHP
php生成不重复随机数、数组的4种方法分享
Mar 30 PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
Sep 23 PHP
thinkPHP中create方法与令牌验证实例浅析
Dec 08 PHP
PHP实现中国公民身份证号码有效性验证示例代码
May 03 PHP
visual studio code 调试php方法(图文详解)
Sep 15 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
Jun 16 PHP
Laravel学习笔记之Artisan命令生成自定义模板的方法
Nov 22 PHP
PHP PDOStatement::closeCursor讲解
Jan 30 PHP
PHPUnit + Laravel单元测试常用技能
Nov 06 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
在Zeus Web Server中安装PHP语言支持
2006/10/09 PHP
用PHP实现多服务器共享SESSION数据的方法
2007/03/16 PHP
PHP array_multisort() 函数的深入解析
2013/06/20 PHP
php实现的九九乘法口诀表简洁版
2014/07/28 PHP
php的GD库imagettftext函数解决中文乱码问题
2015/01/24 PHP
试用php中oci8扩展
2015/06/18 PHP
JavaScript脚本性能优化注意事项
2008/11/18 Javascript
JavaScript类库D
2010/10/24 Javascript
js实现鼠标拖动图片并兼容IE/FF火狐/谷歌等主流浏览器
2013/06/06 Javascript
js定时器的使用(实例讲解)
2014/01/06 Javascript
页面加载完毕后滚动条自动滚动一定位置
2014/02/20 Javascript
使用js画图之正弦曲线
2015/01/12 Javascript
完美解决IE9浏览器出现的对象未定义问题
2016/09/29 Javascript
老生常谈Javascript中的原型和this指针
2016/10/09 Javascript
微信小程序 获取当前地理位置和经纬度实例代码
2016/12/05 Javascript
js移动端图片压缩上传功能
2020/08/18 Javascript
vue项目中使用axios上传图片等文件操作
2017/11/02 Javascript
layui的布局和表格的渲染以及动态生成表格的方法
2019/09/18 Javascript
js 递归json树实现根据子id查父id的方法分析
2019/11/08 Javascript
python使用三角迭代计算圆周率PI的方法
2015/03/20 Python
理解生产者消费者模型及在Python编程中的运用实例
2016/06/26 Python
Python字典数据对象拆分的简单实现方法
2017/12/05 Python
通过Python 获取Android设备信息的轻量级框架
2017/12/18 Python
详解关于Django中ORM数据库迁移的配置
2018/10/08 Python
用Python实现数据的透视表的方法
2018/11/16 Python
python 实现敏感词过滤的方法
2019/01/21 Python
python读写csv文件方法详细总结
2019/07/05 Python
Pytorch提取模型特征向量保存至csv的例子
2020/01/03 Python
pyqt5 QlistView列表显示的实现示例
2020/03/24 Python
利用python绘制数据曲线图的实现
2020/04/09 Python
英国乡村时尚和宠物用品专家:Pet & Country
2018/07/02 全球购物
医学专业五年以上个人求职信
2013/12/03 职场文书
职工代表大会主持词
2014/04/01 职场文书
财务会计专业自荐书
2014/06/30 职场文书
逃课打麻将检讨书
2014/10/05 职场文书
运动会开幕式通讯稿
2015/07/18 职场文书