php calender(日歷)二個(gè)版本代碼示例(解決2038問題)
注意32位機(jī)有2038問題,所以32位服務(wù)器的年限范圍1970年~2038年
我們還可以使用DateTime來規(guī)避這個(gè)問題(這樣與32位64位無關(guān)了)
<?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位機(jī)
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' );
//當(dāng)月最后一天
$lastday = date ( 'j', mktime ( 0, 0, 0, $nextMonth, 0, $year ) );
//循環(huán)輸出天數(shù)
$day = 1;
$line = '';
while ( $day <= $lastday )
{
$cday = $year . '-' . $month . '-' . $day;
//當(dāng)前星期幾
$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>";
//一周結(jié)束
if ($nowWeek == 7)
{
$line .= '</tr>';
$html .= $line;
$line = '<tr align="center">';
}
//全月結(jié)束
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;
}
/**
*
* 檢測(cè)是否是32位機(jī)
* @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' );
//當(dāng)月最后一天
$creatDate = new DateTime("$year-$nextMonth-0");
$lastday = $creatDate->format('j');
$creatDate = NULL;
//循環(huán)輸出天數(shù)
$day = 1;
$line = '';
while ( $day <= $lastday )
{
$cday = $year . '-' . $month . '-' . $day;
//當(dāng)前星期幾
$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>";
//一周結(jié)束
if ($nowWeek == 7)
{
$line .= '</tr>';
$html .= $line;
$line = '<tr align="center">';
}
//全月結(jié)束
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;
}
- 總結(jié)PHP中DateTime的常用方法
- PHP將DateTime對(duì)象轉(zhuǎn)化為友好時(shí)間顯示的實(shí)現(xiàn)代碼
- php日期轉(zhuǎn)時(shí)間戳,指定日期轉(zhuǎn)換成時(shí)間戳
- PHP中UNIX時(shí)間戳和日期間的轉(zhuǎn)換與計(jì)算實(shí)例
- 解析php時(shí)間戳與日期的轉(zhuǎn)換
- PHP時(shí)間戳與日期之間轉(zhuǎn)換的實(shí)例介紹
- php根據(jù)日期或時(shí)間戳獲取星座信息和生肖等信息
- PHP中大于2038年時(shí)間戳的問題處理方案
- 關(guān)于PHP轉(zhuǎn)換超過2038年日期出錯(cuò)的問題解決
- php實(shí)現(xiàn)兼容2038年后Unix時(shí)間戳轉(zhuǎn)換函數(shù)
- PHP基于DateTime類解決Unix時(shí)間戳與日期互轉(zhuǎn)問題【針對(duì)1970年前及2038年后時(shí)間戳】
相關(guān)文章
laravel實(shí)現(xiàn)查詢最后執(zhí)行的一條sql語(yǔ)句的方法
今天小編就為大家分享一篇laravel實(shí)現(xiàn)查詢最后執(zhí)行的一條sql語(yǔ)句的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10Laravel 驗(yàn)證碼認(rèn)證學(xué)習(xí)記錄小結(jié)
這篇文章主要介紹了Laravel 驗(yàn)證碼認(rèn)證學(xué)習(xí)記錄小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12PHP+jQuery 注冊(cè)模塊的改進(jìn)(三):更新到Smarty3.1
本文是PHP+jQuery 注冊(cè)模塊的改進(jìn)的第三篇,主要記錄了講Smarty更新到最新的3.1版本,是篇非常使用的文章,有需要的朋友可以參考下2014-10-10Laravel框架實(shí)現(xiàn)定時(shí)Task Scheduling例子
今天小編就為大家分享一篇Laravel框架實(shí)現(xiàn)定時(shí)Task Scheduling例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP計(jì)算一年多少個(gè)星期和每周的開始和結(jié)束日期
這篇文章主要介紹了PHP計(jì)算每周的開始和結(jié)束日期,php實(shí)現(xiàn)計(jì)算一年多少周,同時(shí)計(jì)算出每一周的開始日期和結(jié)束日期,需要的朋友可以參考下2014-07-07