您好,登錄后才能下訂單哦!
本篇內容介紹了“php calender二個版本代碼的區別有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
注意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 >星期一</td>
<td >星期二</td>
<td >星期三</td>
<td >星期四</td>
<td >星期五</td>
<td >星期六</td>
<td >星期天</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 = '';
} 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 >星期一</td>
<td >星期二</td>
<td >星期三</td>
<td >星期四</td>
<td >星期五</td>
<td >星期六</td>
<td >星期天</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 = '';
} 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 calender二個版本代碼的區別有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。