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程序员工具
May 26 PHP
在php和MySql中计算时间差的方法
Apr 22 PHP
php数组函数序列之in_array() 查找数组值是否存在
Oct 29 PHP
编译php 5.2.14+fpm+memcached(具体操作详解)
Jun 18 PHP
ThinkPHP多表联合查询的常用方法
Mar 24 PHP
php中使用gd库实现下载网页中所有图片
May 12 PHP
两款万能的php分页类
Nov 12 PHP
PHP实现文件上传与下载实例与总结
Mar 13 PHP
Zend Framework教程之Zend_Db_Table用法详解
Mar 21 PHP
PHP的Laravel框架中使用消息队列queue及异步队列的方法
Mar 21 PHP
php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证
May 04 PHP
PHP htmlentities()函数用法讲解
Feb 25 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 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
2013/06/25 PHP
学习php设计模式 php实现合成模式(composite)
2015/12/08 PHP
php网页版聊天软件实现代码
2016/08/12 PHP
thinkPHP框架实现的简单计算器示例
2018/12/07 PHP
JavaScript 学习笔记(十六) js事件
2010/02/01 Javascript
StringTemplate遇见jQuery冲突的解决方法
2011/09/22 Javascript
jquery 文本上下无缝滚动,鼠标放上去就停止 小例子
2013/06/05 Javascript
jQuery动态创建html元素的常用方法汇总
2014/09/05 Javascript
Javascript基础教程之switch语句
2015/01/18 Javascript
Javascript中实现trim()函数的两种方法
2015/02/04 Javascript
用JavaScript实现PHP的urlencode与urldecode函数
2015/08/13 Javascript
angularJS与bootstrap结合实现动态加载弹出提示内容
2015/10/16 Javascript
基于jQuery Ajax实现上传文件
2016/03/24 Javascript
自己封装的一个简单的倒计时功能实例
2016/11/23 Javascript
解析js如何获取css样式
2016/12/11 Javascript
js实现简单的手风琴效果
2017/02/27 Javascript
JavaScript设计模式之缓存代理模式原理与简单用法示例
2018/08/07 Javascript
原生JS生成指定位数的验证码
2020/10/28 Javascript
在Python的框架中为MySQL实现restful接口的教程
2015/04/08 Python
python爬取网页转换为PDF文件
2018/06/07 Python
flask框架url与重定向操作实例详解
2020/01/25 Python
用python打开摄像头并把图像传回qq邮箱(Pyinstaller打包)
2020/05/17 Python
python爬取youtube视频的示例代码
2021/03/03 Python
CSS Grid布局教程之浏览器开启CSS Grid Layout汇总
2014/12/30 HTML / CSS
美国孕妇装购物网站:Motherhood Maternity
2019/09/22 全球购物
宏碁西班牙官网:Acer西班牙
2021/01/08 全球购物
可贵的沉默教学反思
2014/02/06 职场文书
矿泉水广告词
2014/03/20 职场文书
我为党旗添光彩演讲稿
2014/09/13 职场文书
2015年度党员个人总结
2015/02/14 职场文书
2015年学校禁毒工作总结
2015/05/27 职场文书
债务追讨律师函
2015/06/24 职场文书
校园之声广播稿
2015/08/18 职场文书
公开致歉信
2019/06/24 职场文书
天谕手游15杯全调酒配方和调酒券的获得方式
2022/04/06 其他游戏
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫