• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-25) 35次浏览 已收录 0个评论

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

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

<?php<BR>/**<BR> * <BR> * 我的日历<BR> * date_default_timezone_set date mktime<BR> * @param int $year<BR> * @param int $month<BR> * @param string $timezone<BR> * @author fc_lamp<BR> * @blog: fc-lamp.blog.163.com<BR> */<BR>function myCalender($year = '', $month = '', $timezone = 'Asia/Shanghai')<BR>{<br><br> date_default_timezone_set ( $timezone );<BR> $year = abs ( intval ( $year ) );<BR> $month = abs ( intval ( $month ) );<br><br> //是否是32位机<BR> if (is32())<BR> {<BR>  if ($year = 2038)<BR>  {<BR>   $year = date ( 'Y' );<BR>  }<BR> } else<BR> {<BR>  if ($year <= 0)<BR>  {<BR>   $year = date ( 'Y' );<BR>  }<br><br> }<br><br> if ($month  12)<BR> {<BR>  $month = date ( 'm' );<BR> }<br><br> //上一年<BR> $pretYear = $year - 1;<BR> //上一月<BR> $mpYear = $year;<BR> $preMonth = $month - 1;<BR> if ($preMonth <= 0)<BR> {<BR>  $preMonth = 1;<BR>  $mpYear = $pretYear;<BR> }<br><br> //下一年<BR> $nextYear = $year + 1;<BR> //下一月<BR> $mnYear = $year;<BR> $nextMonth = $month + 1;<BR> if ($nextMonth > 12)<BR> {<BR>  $nextMonth = 1;<BR>  $mnYear = $nextYear;<BR> }<br><br> //日历头<BR> $html = <<<HTML<BR><table width="500" border="1"><BR>  <tr align="center"><BR>    <td>上一年</td><BR>    <td>上一月</td><BR>     <td>回到今天</td><BR>    <td>下一月</td><BR> <td>下一年</td><BR>  </tr><BR>  <tr align="center"><BR>    <td colspan="5">{$year}年{$month}月</td><BR>  </tr><BR>  <tr><BR>   <td colspan="5"><BR>  <table width="100%" border="1"><BR>   <tr align="center"><BR>    <td style="background-color:#DAF0DD">星期一</td><BR>    <td style="background-color:#DAF0DD">星期二</td><BR>    <td style="background-color:#DAF0DD">星期三</td><BR>    <td style="background-color:#DAF0DD">星期四</td><BR>    <td style="background-color:#DAF0DD">星期五</td><BR>    <td style="background-color:#F60;color:#fff;font-weight: bold">星期六</td><BR>    <td style="background-color:#F60;color:#fff;font-weight: bold">星期天</td><BR>   </tr><BR>HTML;<br><br> $currentDay = date ( 'Y-m-j' );<br><br> //当月最后一天<BR> $lastday = date ( 'j', mktime ( 0, 0, 0, $nextMonth, 0, $year ) );<br><br> //循环输出天数<BR> $day = 1;<BR> $line = '';<BR> while ( $day <= $lastday )<BR> {<BR>  $cday = $year . '-' . $month . '-' . $day;<br><br>  //当前星期几<BR>  $nowWeek = date ( 'N', mktime ( 0, 0, 0, $month, $day, $year ) );<br><br>  if ($day == 1)<BR>  {<BR>   $line = '<tr align="center">';<BR>   $line .= str_repeat ( '<td> </td>', $nowWeek - 1 );<BR>  }<br><br>  if ($cday == $currentDay)<BR>  {<BR>   $style = 'style="color:red;"';<BR>  } else<BR>  {<BR>   $style = '';<BR>  }<br><br>  $line .= "<td>$day</td>";<br><br>  //一周结束<BR>  if ($nowWeek == 7)<BR>  {<BR>   $line .= '</tr>';<BR>   $html .= $line;<BR>   $line = '<tr align="center">';<BR>  }<br><br>  //全月结束<BR>  if ($day == $lastday)<BR>  {<BR>   if ($nowWeek != 7)<BR>   {<BR>    $line .= str_repeat ( '<td> </td>', 7 - $nowWeek );<BR>   }<BR>   $line .= '</tr>';<BR>   $html .= $line;<br><br>   break;<BR>  }<br><br>  $day ++;<BR> }<br><br> $html .= <<<HTML<BR>  </table> <BR> </td><BR>  </tr><BR></table><BR>HTML;<BR> return $html;<BR>}</P><P>/**<BR> * <BR> * 检测是否是32位机<BR> * @author fc_lamp<BR> * @blog: fc-lamp.blog.163.com<BR> */<BR>function is32()<BR>{<BR> $is32 = False;<BR> if (strtotime ( '2039-10-10' ) === False)<BR> {<BR>  $is32 = True;<BR> }<BR> return $is32;<BR>}<BR>

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

<?php<BR>/**<BR> * <BR> * 我的日历(DateTime版本)<BR> * date_default_timezone_set date mktime<BR> * @param int $year<BR> * @param int $month<BR> * @param string $timezone<BR> * @author fc_lamp<BR> * @blog: fc-lamp.blog.163.com<BR> */<BR>function myCalender($year = '', $month = '', $timezone = 'Asia/Shanghai')<BR>{<br><br> date_default_timezone_set ( $timezone );<BR> $year = abs ( intval ( $year ) );<BR> $month = abs ( intval ( $month ) );<br><br> $nowDate = new DateTime();<br><br> if ($year <= 0)<BR> {<BR>  $year = $nowDate->format( 'Y' );<BR> }<br><br> if ($month  12)<BR> {<BR>  $month = $nowDate->format('m' );<BR> }<br><br> //上一年<BR> $pretYear = $year - 1;<BR> //上一月<BR> $mpYear = $year;<BR> $preMonth = $month - 1;<BR> if ($preMonth <= 0)<BR> {<BR>  $preMonth = 1;<BR>  $mpYear = $pretYear;<BR> }<br><br> //下一年<BR> $nextYear = $year + 1;<BR> //下一月<BR> $mnYear = $year;<BR> $nextMonth = $month + 1;<BR> if ($nextMonth > 12)<BR> {<BR>  $nextMonth = 1;<BR>  $mnYear = $nextYear;<BR> }<br><br> //日历头<BR> $html = <<<HTML<BR><table width="500" border="1"><BR>  <tr align="center"><BR>    <td>上一年</td><BR>    <td>上一月</td><BR>     <td>回到今天</td><BR>    <td>下一月</td><BR> <td>下一年</td><BR>  </tr><BR>  <tr align="center"><BR>    <td colspan="5">{$year}年{$month}月</td><BR>  </tr><BR>  <tr><BR>   <td colspan="5"><BR>  <table width="100%" border="1"><BR>   <tr align="center"><BR>    <td style="background-color:#DAF0DD">星期一</td><BR>    <td style="background-color:#DAF0DD">星期二</td><BR>    <td style="background-color:#DAF0DD">星期三</td><BR>    <td style="background-color:#DAF0DD">星期四</td><BR>    <td style="background-color:#DAF0DD">星期五</td><BR>    <td style="background-color:#F60;color:#fff;font-weight: bold">星期六</td><BR>    <td style="background-color:#F60;color:#fff;font-weight: bold">星期天</td><BR>   </tr><BR>HTML;<br><br> $currentDay = $nowDate->format('Y-m-j' );<br><br> //当月最后一天<BR> $creatDate = new DateTime("$year-$nextMonth-0");<BR> $lastday = $creatDate->format('j');<BR> $creatDate = NULL;<br><br> //循环输出天数<BR> $day = 1;<BR> $line = '';<BR> while ( $day <= $lastday )<BR> {<BR>  $cday = $year . '-' . $month . '-' . $day;<br><br>  //当前星期几<BR>  $creatDate = new DateTime("$year-$month-$day");<BR>  $nowWeek = $creatDate->format('N');<BR>  $creatDate = NULL;<br><br>  if ($day == 1)<BR>  {<BR>   $line = '<tr align="center">';<BR>   $line .= str_repeat ( '<td> </td>', $nowWeek - 1 );<BR>  }<br><br>  if ($cday == $currentDay)<BR>  {<BR>   $style = 'style="color:red;"';<BR>  } else<BR>  {<BR>   $style = '';<BR>  }<br><br>  $line .= "<td>$day</td>";<br><br>  //一周结束<BR>  if ($nowWeek == 7)<BR>  {<BR>   $line .<strong>*本文来源gaodai#ma#com搞@代~码^网+</strong><strong>搞代gaodaima码</strong>= '</tr>';<BR>   $html .= $line;<BR>   $line = '<tr align="center">';<BR>  }<br><br>  //全月结束<BR>  if ($day == $lastday)<BR>  {<BR>   if ($nowWeek != 7)<BR>   {<BR>    $line .= str_repeat ( '<td> </td>', 7 - $nowWeek );<BR>   }<BR>   $line .= '</tr>';<BR>   $html .= $line;<br><br>   break;<BR>  }<br><br>  $day ++;<BR> }<br><br> $html .= <<<HTML<BR>  </table> <BR> </td><BR>  </tr><BR></table><BR>HTML;<BR> return $html;<BR>}<BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php calender(日历)二个版本代码示例(解决2038问题)_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址