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 相关文章推荐
phpmyadmin MySQL 加密配置方法
Jul 05 PHP
php学习之 循环结构实现代码
Jun 09 PHP
php 文件上传实例代码
Apr 19 PHP
php使用mkdir创建多级目录入门例子
May 10 PHP
PHP结合JQueryJcrop实现图片裁切实例详解
Jul 24 PHP
PHP字符串比较函数strcmp()和strcasecmp()使用总结
Nov 19 PHP
php和editplus正则表达式去除空白行
Apr 17 PHP
php把数组值转换成键的方法
Jul 13 PHP
PHP数据库操作二:memcache用法分析
Aug 16 PHP
PHP生成腾讯云COS接口需要的请求签名
May 20 PHP
Aliyun Linux 编译安装 php7.3 tengine2.3.2 mysql8.0 redis5的过程详解
Oct 20 PHP
深入解析PHP底层机制及相关原理
Dec 11 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格式化工具Beautify PHP小小BUG
2008/04/24 PHP
php生成缩略图的类代码
2008/10/02 PHP
Linux下php5.4启动脚本
2014/08/03 PHP
PHP中绘制图像的一些函数总结
2014/11/19 PHP
Joomla调用系统自带编辑器的实现方法
2016/05/05 PHP
实例化php类时传参的方法分析
2020/06/05 PHP
10个实用的脚本代码工具
2010/05/04 Javascript
在页面上用action传递参数到后台出现乱码的解决方法
2013/12/31 Javascript
AngularJS向后端ASP.NET API控制器上传文件
2016/02/03 Javascript
JavaScipt选取文档元素的方法(推荐)
2016/08/05 Javascript
NodeJS实现微信公众号关注后自动回复功能
2017/05/31 NodeJs
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
jQuery访问浏览器本地存储cookie、localStorage和sessionStorage的基本用法
2017/10/20 jQuery
angular之ng-template模板加载
2017/11/09 Javascript
使用D3.js构建实时图形的示例代码
2018/08/28 Javascript
JavaScript格式化json和xml的方法示例
2019/01/22 Javascript
微信小程序获取用户信息的两种方法wx.getUserInfo与open-data实例分析
2019/05/03 Javascript
[01:45]DOTA2新英雄“神谕者”全方位展示
2014/11/21 DOTA
python传递参数方式小结
2015/04/17 Python
Django使用httpresponse返回用户头像实例代码
2018/01/26 Python
Python元组知识点总结
2019/02/18 Python
Python3内置模块之base64编解码方法详解
2019/07/13 Python
Python爬虫过程解析之多线程获取小米应用商店数据
2020/11/14 Python
美国校园市场:OCM
2017/06/08 全球购物
公司年会晚宴演讲稿
2014/01/06 职场文书
汽车销售员如何做职业生涯规划
2014/02/16 职场文书
餐厅楼面部长岗位职责范文
2014/02/16 职场文书
文科毕业生自荐书范文
2014/04/17 职场文书
三八妇女节演讲稿
2014/05/27 职场文书
2014班子成员自我剖析材料思想汇报
2014/10/01 职场文书
旷课检讨书500字
2014/10/14 职场文书
研究生给导师的自荐信
2015/03/06 职场文书
通知函的格式
2015/04/27 职场文书
家电创业计划书
2019/08/05 职场文书
Go遍历struct,map,slice的实现
2021/06/13 Golang
详解Redis的三种常用的缓存读写策略步骤
2022/05/06 Redis