服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - php calender(日历)二个版本代码示例(解决2038问题)

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

2020-06-02 12:09PHP教程网 PHP教程

一个简单的php Calender(日历),解决了2038问题,这样在32位机和64位机上都可以用了,代码很简单,方便修改

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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</td>', 7 - $nowWeek );
   }
   $line .= '</tr>';
   $html .= $line;

   break;
  }

  $day ++;
 }

 $html .= <<<HTML
  </table> 
 </td>
  </tr>
</table>
HTML;
 return $html;
}

延伸 · 阅读

精彩推荐
  • PHP教程php数据结构之顺序链表与链式线性表示例

    php数据结构之顺序链表与链式线性表示例

    这篇文章主要介绍了php数据结构之顺序链表与链式线性表,结合实例形式较为详细的分析了php实现顺序链表与链式线性表的各种常用操作技巧,需要的朋友可...

    2cto3282019-10-24
  • PHP教程PHP如何抛出异常处理错误

    PHP如何抛出异常处理错误

    PHP 5 提供了一种新的面向对象的错误处理方法。异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程。这种情况称为异常。 ...

    PHP技术网2642019-11-18
  • PHP教程PHP 获取客户端 IP 地址的方法实例代码

    PHP 获取客户端 IP 地址的方法实例代码

    这篇文章主要介绍了PHP 获取客户端 IP 地址的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下...

    且听の风吟4582019-08-29
  • PHP教程PHP耦合设计模式实例分析

    PHP耦合设计模式实例分析

    这篇文章主要介绍了PHP耦合设计模式,结合实例形式分析了php面向对象程序设计中降低耦合度的相关操作技巧,需要的朋友可以参考下 ...

    山雨欲来-风满楼3482019-09-14
  • PHP教程php生成word并下载代码实例

    php生成word并下载代码实例

    这篇文章主要介绍了php生成word并下载代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    下页、再停留2342019-05-30
  • PHP教程PHP获取ttf格式文件字体名的方法示例

    PHP获取ttf格式文件字体名的方法示例

    这篇文章主要介绍了PHP获取ttf格式文件字体名的方法,结合实例形式分析了php读取ttf字体文件属性的相关操作技巧,需要的朋友可以参考下...

    老虎会游泳3642019-06-05
  • PHP教程PHP的PDO预处理语句与存储过程

    PHP的PDO预处理语句与存储过程

    今天小编就为大家分享一篇关于PHP的PDO预处理语句与存储过程,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编...

    php参考手册2512019-06-27
  • PHP教程php 备份数据库代码(生成word,excel,json,xml,sql)

    php 备份数据库代码(生成word,excel,json,xml,sql)

    本篇文章是对php备份数据库代码(生成word,excel,json,xml,sql)进行了详细的分析介绍,需要的朋友参考下 ...

    PHP教程网4782020-04-26