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 相关文章推荐
提取HTML标签
Oct 09 PHP
模拟OICQ的实现思路和核心程序(三)
Oct 09 PHP
第九节--绑定
Nov 16 PHP
eWebEditor v3.8 商业完整版 (PHP)
Dec 06 PHP
使用session判断用户登录用户权限(超简单)
Jun 08 PHP
php检测图片主要颜色的方法
Jul 01 PHP
php实现微信公众号主动推送消息
Dec 31 PHP
Zend Framework生成验证码并实现验证码验证功能(附demo源码下载)
Mar 22 PHP
thinkphp 验证码 的使用小结
May 07 PHP
基于php流程控制语句和循环控制语句(讲解)
Oct 23 PHP
php JWT在web端中的使用方法教程
Sep 06 PHP
PHP __call()方法实现委托示例
May 20 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
删除及到期域名的查看(抢域名必备哦)
2008/05/14 PHP
介绍php设计模式中的工厂模式
2008/06/12 PHP
php 数组二分法查找函数代码
2010/02/16 PHP
解析thinkphp的左右值无限分类
2013/06/20 PHP
解析php file_exists无效的解决办法
2013/06/26 PHP
PHP模板引擎Smarty的缓存使用总结
2014/04/24 PHP
PHP封装的字符串加密解密函数
2015/12/18 PHP
PHP常见错误提示含义解释(实用!值得收藏)
2016/04/25 PHP
use jscript Create a SQL Server database
2007/06/16 Javascript
javascript 火狐(firefox)不显示本地图片问题解决
2008/07/05 Javascript
jQuery 剧场版 你必须知道的javascript
2009/05/27 Javascript
JS 表单验证大全
2011/11/23 Javascript
jQuery响应鼠标事件并隐藏与显示input默认值
2014/08/24 Javascript
学习javascript面向对象 实例讲解面向对象选项卡
2016/01/04 Javascript
Bootstrap3 datetimepicker控件使用实例
2016/12/13 Javascript
JS实现获取来自百度,Google,soso,sogou关键词的方法
2016/12/21 Javascript
微信小程序 数据交互与渲染实例详解
2017/01/21 Javascript
在bootstrap中实现轮播图实例代码
2017/06/11 Javascript
浅谈Emergence.js 检测元素可见性的 js 插件
2017/11/18 Javascript
Vue项目服务器部署之子目录部署方法
2019/05/12 Javascript
详解vue项目中实现图片裁剪功能
2019/06/07 Javascript
Vue3.0 响应式系统源码逐行分析讲解
2019/10/14 Javascript
vue引入静态js文件的方法
2020/06/20 Javascript
vue结合el-upload实现腾讯云视频上传功能
2020/07/01 Javascript
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
python如何删除文件中重复的字段
2019/07/16 Python
python3 mmh3安装及使用方法
2019/10/09 Python
Python数据结构dict常用操作代码实例
2020/03/12 Python
Pycharm激活码激活两种快速方式(附最新激活码和插件)
2020/03/12 Python
css3学习心得分享
2013/08/19 HTML / CSS
互联网电子商务专业毕业生求职信
2014/03/18 职场文书
本科毕业生应聘求职信
2014/07/06 职场文书
结对共建协议书
2014/08/20 职场文书
建设办主任四风问题整改思路和措施
2014/09/20 职场文书
故意伤害辩护词
2015/05/21 职场文书
小学教代会开幕词
2016/03/04 职场文书