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树的代码,可以嵌套任意层
Oct 09 PHP
php简单提示框alert封装函数
Aug 08 PHP
PHP中使用cURL实现Get和Post请求的方法
Mar 13 PHP
PHP保留两位小数并且四舍五入及不四舍五入的方法
Sep 22 PHP
Thinkphp搜索时首页分页和搜索页保持条件分页的方法
Dec 05 PHP
Zend Framework教程之Application用法实例详解
Mar 14 PHP
详解PHP实现异步调用的4种方法
Mar 14 PHP
Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
Mar 21 PHP
php 实现301重定向跳转实例代码
Jul 18 PHP
PHP url的pathinfo模式加载不同控制器的简单实现
Aug 12 PHP
php 使用html5实现多文件上传实例
Oct 24 PHP
PHP判断函数是否被定义的方法
Jun 21 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
IP138 IP地址查询小偷实现代码
2010/02/15 PHP
php数组声明、遍历、数组全局变量使用小结
2013/06/05 PHP
PHP实现基于文本的摩斯电码生成器
2016/01/11 PHP
ThinkPHP+EasyUI之ComboTree中的会计科目树形菜单实现方法
2017/06/09 PHP
客户端 使用XML DOM加载json数据的方法
2010/09/28 Javascript
通过js动态操作table(新增,删除相关列信息)
2012/05/23 Javascript
利用js实现选项卡的特别效果的实例
2013/03/03 Javascript
基于JavaScript 类的使用详解
2013/05/07 Javascript
浅析jQuery中常用的元素查找方法总结
2013/07/04 Javascript
JS+JSP通过img标签调用实现静态页面访问次数统计的方法
2015/12/14 Javascript
JavaScript编写Chrome扩展实现与浏览器的交互及时间通知
2016/05/16 Javascript
JavaScript中点击事件的写法
2016/06/28 Javascript
AngularJS基础 ng-list 指令详解及示例代码
2016/08/02 Javascript
JavaScript的事件机制详解
2017/01/17 Javascript
JavaScript中的编码和解码函数
2017/02/15 Javascript
无循环 JavaScript(map、reduce、filter和find)
2017/04/08 Javascript
自定义vue组件发布到npm的方法
2018/05/09 Javascript
JavaScript接口实现方法实例分析
2020/05/16 Javascript
解决vue侦听器watch,调用this时出现undefined的问题
2020/10/30 Javascript
Vue+Spring Boot简单用户登录(附Demo)
2020/11/12 Javascript
python的id()函数解密过程
2012/12/25 Python
Python的Bottle框架的一些使用技巧介绍
2015/04/08 Python
Python中用Spark模块的使用教程
2015/04/13 Python
Python元组操作实例分析【创建、赋值、更新、删除等】
2017/07/24 Python
Python设计实现的计算器功能完整实例
2017/08/18 Python
Python实现生成随机数据插入mysql数据库的方法
2017/12/25 Python
Python实现定时执行任务的三种方式简单示例
2019/03/30 Python
PyCharm安装Markdown插件的两种方法
2019/06/24 Python
如何通过50行Python代码获取公众号全部文章
2019/07/12 Python
使用python计算三角形的斜边例子
2020/04/15 Python
基于python实现matlab filter函数过程详解
2020/06/08 Python
介绍Java的内部类
2012/10/27 面试题
写一个函数返回1+2+3+…+n的值(假定结果不会超过长整型变量的范围)
2014/09/05 面试题
总裁办公室主任职责
2014/01/02 职场文书
新郎婚礼答谢词
2015/01/04 职场文书
2015年“世界无车日”活动方案
2015/05/06 职场文书