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学习资源和链接.
Dec 05 PHP
php 远程图片保存到本地的函数类
Dec 08 PHP
window+nginx+php环境配置 附配置搭配说明
Dec 29 PHP
一个基于PDO的数据库操作类(新) 一个PDO事务实例
Jul 03 PHP
php中unlink()、mkdir()、rmdir()等方法的使用介绍
Dec 21 PHP
解析获取优酷视频真实下载地址的PHP源代码
Jun 26 PHP
PHP 验证登陆类分享
Mar 13 PHP
php实现在限定区域里自动调整字体大小的类实例
Apr 02 PHP
PHP实现简单搜歌的方法
Jul 28 PHP
100多行PHP代码实现socks5代理服务器[2]
May 05 PHP
PHP count()函数讲解
Feb 03 PHP
PHP检查URL包含特定字符串实例方法
Feb 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
收音机发烧友应当熟知的100条知识
2021/03/02 无线电
php 分库分表hash算法
2009/11/12 PHP
PHP实现的数独求解问题示例
2017/04/18 PHP
php合并数组并保留键值的实现方法
2018/03/12 PHP
PHP操作redis实现的分页列表,新增,删除功能封装类与用法示例
2018/08/04 PHP
php屏蔽错误及提示的方法
2020/05/10 PHP
JS的IE和Firefox兼容性集锦
2006/12/11 Javascript
使用jquery实现图文切换效果另加特效
2013/01/20 Javascript
form表单中去掉默认的enter键提交并绑定js方法实现代码
2013/04/01 Javascript
JS定时刷新页面及跳转页面的方法
2013/07/04 Javascript
Vue 项目分环境打包的方法示例
2018/08/03 Javascript
js console.log打印对象时属性缺失的解决方法
2019/05/23 Javascript
javascript实现导航栏分页效果
2019/06/27 Javascript
vue项目初始化到登录login页面的示例
2019/10/31 Javascript
Vue通过getAction的finally来最大程度避免影响主数据呈现问题
2020/04/24 Javascript
[00:37]2016完美“圣”典风云人物:AMS宣传片
2016/12/06 DOTA
[02:19]DOTA选手解说齐贺岁
2018/02/11 DOTA
Python实现子类调用父类的方法
2014/11/10 Python
Python Web框架Flask中使用百度云存储BCS实例
2015/02/08 Python
Python 判断图像是否读取成功的方法
2019/01/26 Python
python scatter散点图用循环分类法加图例
2019/03/19 Python
Python程序打包工具py2exe和PyInstaller详解
2019/06/28 Python
Flask框架模板渲染操作简单示例
2019/07/31 Python
django中上传图片分页三级联动效果的实现代码
2019/08/30 Python
python跨文件使用全局变量的实现
2020/11/17 Python
利用SVG和CSS3来实现一个炫酷的边框动画
2015/07/22 HTML / CSS
芬兰攀岩、山地运动和户外活动用品购物网站:Bergfreunde
2016/10/06 全球购物
澳大利亚电子产品购物网站:Dick Smith
2017/02/02 全球购物
英国时尚服饰电商:Boohoo
2017/10/12 全球购物
世界上最大的冷却器制造商:Igloo Coolers
2019/07/23 全球购物
2014年优质护理服务工作总结
2014/11/14 职场文书
2016三严三实专题教育活动心得体会
2016/01/06 职场文书
2016年党风廉政建设承诺书
2016/03/25 职场文书
古诗之感恩老师
2019/10/24 职场文书
Python import模块的缓存问题解决方案
2021/06/02 Python
MySQL创建管理子分区
2022/04/13 MySQL