php calender(日历)二个版本代码示例(解决2038问题)


Posted in PHP onDecember 24, 2013

php calender(日历)二个版本代码示例(解决2038问题)

注意32位机有2038问题,所以32位服务器的年限范围1970年~2038年

我们还可以使用DateTime来规避这个问题(这样与32位64位无关了)

<?php
/**
 * 
 * 我的日历
 * date_default_timezone_set date mktime
 * @param int $year
 * @param int $month
 * @param string $timezone
 * @author fc_lamp
 * @blog: fc-lamp.blog.163.com
 */
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;
}

使用DateTime 类解决2038问题,这样不分32位与64位,代码如下:

<?php
/**
 * 
 * 我的日历(DateTime版本)
 * date_default_timezone_set date mktime
 * @param int $year
 * @param int $month
 * @param string $timezone
 * @author fc_lamp
 * @blog: fc-lamp.blog.163.com
 */
function myCalender($year = '', $month = '', $timezone = 'Asia/Shanghai')
{ date_default_timezone_set ( $timezone );
 $year = abs ( intval ( $year ) );
 $month = abs ( intval ( $month ) );
 $nowDate = new DateTime();
 if ($year <= 0)
 {
  $year = $nowDate->format( 'Y' );
 }
 if ($month <= 0 or $month > 12)
 {
  $month = $nowDate->format('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 = $nowDate->format('Y-m-j' );
 //当月最后一天
 $creatDate = new DateTime("$year-$nextMonth-0");
 $lastday = $creatDate->format('j');
 $creatDate = NULL;
 //循环输出天数
 $day = 1;
 $line = '';
 while ( $day <= $lastday )
 {
  $cday = $year . '-' . $month . '-' . $day;
  //当前星期几
  $creatDate = new DateTime("$year-$month-$day");
  $nowWeek = $creatDate->format('N');
  $creatDate = NULL;
  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;
}
PHP 相关文章推荐
PHP与MySQL交互使用详解
Oct 09 PHP
php session 错误
May 21 PHP
具有时效性的php加密解密函数代码
Jun 19 PHP
php检测数组长度函数sizeof与count用法
Nov 17 PHP
浅谈PHP中Stream(流)
Jun 08 PHP
PHP实现无限分类的实现方法
Nov 14 PHP
浅谈php中的访问修饰符private、protected、public的作用范围
Nov 20 PHP
thinkphp实现把数据库中的列的值存到下拉框中的方法
Jan 20 PHP
浅析php如何实现爬取数据原理
Sep 27 PHP
PHP7创建COOKIE和销毁COOKIE的实例方法
Feb 03 PHP
php将字符串转换为数组实例讲解
May 05 PHP
基于php+MySql实现学生信息管理系统实例
Aug 04 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
Dec 24 #PHP
phpexcel导入excel数据使用方法实例
Dec 24 #PHP
php生成excel列序号代码实例
Dec 24 #PHP
php jquery 多文件上传简单实例
Dec 23 #PHP
php安装xdebug/php安装pear/phpunit详解步骤(图)
Dec 22 #PHP
PHP变量的定义、可变变量、变量引用、销毁方法
Dec 20 #PHP
部署PHP项目应该注意的几点事项分享
Dec 20 #PHP
You might like
PHP实现RTX发送消息提醒的实例代码
2017/01/03 PHP
php使用redis的有序集合zset实现延迟队列应用示例
2020/02/20 PHP
小议Javascript中的this指针
2010/03/18 Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
2011/03/07 Javascript
capacityFixed 基于jquery的类似于新浪微博新消息提示的定位框
2011/05/24 Javascript
javascript通过className来获取元素的简单示例代码
2014/01/10 Javascript
javascript动画算法实例分析
2015/07/31 Javascript
javascript中return,return true,return false三者的用法及区别
2015/11/17 Javascript
深入浅析Node.js单线程模型
2017/07/10 Javascript
Vue2.0基于vue-cli+webpack同级组件之间的通信教程(推荐)
2017/09/14 Javascript
浅谈angular.copy() 深拷贝
2017/09/14 Javascript
vue自动路由-单页面项目(非build时构建)
2019/04/30 Javascript
微信小程序tabBar 返回tabBar不刷新页面
2019/07/25 Javascript
layui table去掉右侧滑动条的实现方法
2019/09/05 Javascript
javascript实现京东登录显示隐藏密码
2020/08/02 Javascript
[01:10:48]完美世界DOTA2联赛PWL S2 GXR vs PXG 第一场 11.18
2020/11/18 DOTA
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
2016/10/12 Python
Django读取Mysql数据并显示在前端的实例
2018/05/27 Python
python MNIST手写识别数据调用API的方法
2018/08/08 Python
python利用Opencv实现人脸识别功能
2019/04/25 Python
python-OpenCV 实现将数组转换成灰度图和彩图
2020/01/09 Python
Python爬取新型冠状病毒“谣言”新闻进行数据分析
2020/02/16 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
一篇文章教你用python画动态爱心表白
2020/11/22 Python
瑜伽服装品牌:露露柠檬(lululemon athletica)
2017/06/04 全球购物
一道SQL面试题
2012/12/31 面试题
用C#语言写出在本地创建一个UDP接收端口的具体过程
2016/02/22 面试题
《鱼游到了纸上》教学反思
2014/02/20 职场文书
初三学生评语大全
2014/04/24 职场文书
高中综合实践活动总结
2014/07/07 职场文书
民事和解协议书格式
2014/11/29 职场文书
2015年检察院个人工作总结
2015/05/20 职场文书
幼儿园老师工作总结2015
2015/05/22 职场文书
Python中的 Set 与 dict
2022/03/13 Python
【TED出品】天梯非主流开心游1700 划水骑士
2022/03/31 魔兽争霸
linux目录管理方法介绍
2022/06/01 Servers