php calender(日曆)二個版本程式碼範例(解決2038問題)_PHP教程

來源:互聯網
上載者:User


注意32位機有2038問題,所以32位伺服器的年限範圍1970年~2038年

我們還可以使用DateTime來規避這個問題(這樣與32位64位無關了)

複製代碼 代碼如下:
/**
*
* 我的日曆
* 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 = <<















上一年 上一月 回到今天 下一月 下一年
{$year}年{$month}月





































































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 = ' '; $line .= str_repeat ( ' ', $nowWeek - 1 ); } if ($cday == $currentDay) { $style = 'style="color:red;"'; } else { $style = ''; } $line .= " "; //一周結束 if ($nowWeek == 7) { $line .= ' '; $html .= $line; $line = ' '; } //全月結束 if ($day == $lastday) { if ($nowWeek != 7) { $line .= str_repeat ( ' ', 7 - $nowWeek ); } $line .= ' '; $html .= $line; break; } $day ++; } $html .= <<
星期一 星期二 星期三 星期四 星期五 星期六 星期天
$day


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位,代碼如下:

複製代碼 代碼如下:
/**
*
* 我的日曆(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 = <<















上一年 上一月 回到今天 下一月 下一年
{$year}年{$month}月









































































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 = ' '; $line .= str_repeat ( ' ', $nowWeek - 1 ); } if ($cday == $currentDay) { $style = 'style="color:red;"'; } else { $style = ''; } $line .= " "; //一周結束 if ($nowWeek == 7) { $line .= ' '; $html .= $line; $line = ' '; } //全月結束 if ($day == $lastday) { if ($nowWeek != 7) { $line .= str_repeat ( ' ', 7 - $nowWeek ); } $line .= ' '; $html .= $line; break; } $day ++; } $html .= <<
星期一 星期二 星期三 星期四 星期五 星期六 星期天
$day


HTML;
return $html;
}

http://www.bkjia.com/PHPjc/621670.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/621670.htmlTechArticle注意32位機有2038問題,所以32位伺服器的年限範圍1970年~2038年 我們還可以使用DateTime來規避這個問題(這樣與32位64位無關了) 複製代碼 代碼如...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.