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 生成饼图 三维饼图
Sep 28 PHP
批量去除PHP文件中bom的PHP代码
Mar 13 PHP
ecshop 批量上传(加入自定义属性)
Mar 20 PHP
php 计划任务 检测用户连接状态
Mar 29 PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 PHP
修改ThinkPHP缓存为Memcache的方法
Jun 25 PHP
php计算两个日期相差天数的方法
Mar 14 PHP
PHP中把对象转换为关联数组代码分享
Apr 09 PHP
WordPress中用于获取及自定义头像图片的PHP脚本详解
Dec 17 PHP
ThinkPHP实现登录退出功能
Jun 29 PHP
使用PHP访问RabbitMQ消息队列的方法示例
Jun 06 PHP
Laravel 手动开关 Eloquent 修改器的操作方法
Dec 30 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
漂亮但不安全的CTB
2006/10/09 PHP
CodeIgniter自定义控制器MY_Controller用法分析
2016/01/20 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
Yii框架组件的事件机制原理与用法分析
2020/04/07 PHP
JS自动适应的图片弹窗实例
2013/06/29 Javascript
jquery操作下拉列表、文本框、复选框、单选框集合(收藏)
2014/01/08 Javascript
javascript中数组的多种定义方法和常用函数简介
2014/05/09 Javascript
jQuery实现动画效果circle实例
2015/08/06 Javascript
jsp 网站引入外部css或者js失效问题解决
2016/10/31 Javascript
slideToggle+slideup实现手机端折叠菜单效果
2017/05/25 Javascript
极简主义法编写JavaScript类
2017/11/02 Javascript
浅谈Vuex@2.3.0 中的 state 支持函数申明
2017/11/22 Javascript
使用Vue-scroller页面input框不能触发滑动的问题及解决方法
2020/08/08 Javascript
基于wxpython开发的简单gui计算器实例
2015/05/30 Python
Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
2018/03/15 Python
Python3对称加密算法AES、DES3实例详解
2018/12/06 Python
Python计算时间间隔(精确到微妙)的代码实例
2019/02/26 Python
PyQt5根据控件Id获取控件对象的方法
2019/06/25 Python
Python网页解析器使用实例详解
2020/05/30 Python
Python 通过爬虫实现GitHub网页的模拟登录的示例代码
2020/08/17 Python
SpringBoot首页设置解析(推荐)
2021/02/11 Python
复古斯堪的纳维亚儿童服装:Baby go Retro
2017/09/09 全球购物
linux面试相关问题
2012/08/11 面试题
个人找工作求职简历的自我评价
2013/10/20 职场文书
小学清明节活动方案
2014/03/08 职场文书
学术诚信承诺书
2014/05/26 职场文书
领导班子四风问题个人对照检查材料
2014/10/04 职场文书
县政府班子个人对照检查材料
2014/10/05 职场文书
迎新生欢迎词
2015/01/23 职场文书
公司内部升职自荐信
2015/03/27 职场文书
财务部岗位职责范本
2015/04/14 职场文书
我是特种兵观后感
2015/06/11 职场文书
python字符串的多行输出的实例详解
2021/06/08 Python
用Python实现屏幕截图详解
2022/01/22 Python
移除Selenium中window.navigator.webdriver值
2022/06/10 Python
react中useState使用:如何实现在当前表格直接更改数据
2022/08/05 Javascript