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

前端之家收集整理的这篇文章主要介绍了php calender(日历)二个版本代码示例(解决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 = <<
$line .= str_repeat ( '}if ($cday == $currentDay)
{
$style = 'style="color:red;"';
} else
{
$style = '';
}$line .= "
}//全月结束
if ($day == $lastday)
{
if ($nowWeek != 7)
{
$line .= str_repeat ( '}
$line .= '$html .= $line;break;
}$day ++;
}$html .= << 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 = '',$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;$currentDay = date ( 'Y-m-j' );//当月最后一天
$lastday = date ( 'j',mktime ( 0,$nextMonth,$year ) );//循环输出天数
$day = 1;
$line = '';
while ( $day <= $lastday )
{
$cday = $year . '-' . $month . '-' . $day;//当前星期几
$nowWeek = date ( 'N',$month,$day,$year ) );if ($day == 1)
{
$line = '
$dayif ($nowWeek == 7)
{
$line .= '$html .= $line;
$line = '
$line .= str_repeat ( '}
$line .= '$html .= $line;break;
}$day ++;
}$html .= << HTML;
return $html;
}

原文链接:https://www.f2er.com/php/25357.html

猜你在找的PHP相关文章

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 = '