Home > Backend Development > PHP Tutorial > PHP calender (calendar) two version code examples (solve the 2038 problem)_PHP tutorial

PHP calender (calendar) two version code examples (solve the 2038 problem)_PHP tutorial

WBOY
Release: 2016-07-13 17:18:28
Original
2117 people have browsed it

PHP calender (calendar) two version code examples (solve the 2038 problem)_PHP tutorial

Note that 32-bit machines have a 2038 problem, so the age range of 32-bit servers is 1970~2038

We can also use DateTime to avoid this problem (this has nothing to do with 32-bit and 64-bit)

Copy code The code is as follows:

/**
 *
 * 我的日历
 * 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 = '';
  }

//End of the month
if ($day == $lastday)
{
if ($nowWeek != 7)
{
$line .= str_repeat ( '', 7 - $nowWeek );
}
$line .= '';
$html .= $line;

break;
}

$day ++;
}

$html .= <<
星期一星期二星期三星期四星期五星期六星期天
 $day


HTML;
return $html;
}

/**
*
* Check if it is a 32-bit machine
* @author fc_lamp
* @blog: fc-lamp.blog.163.com
*/
function is32()
{
$is32 = False;
if (strtotime ( '2039-10-10' ) === False)
{
$is32 = True;
}
return $is32;
}

Use the DateTime class to solve the 2038 problem, so that there is no distinction between 32-bit and 64-bit. The code is as follows:

Copy code The code is as follows:

/**
*
* My calendar (DateTime version)
* 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 = '';
  }

//End of the month
if ($day == $lastday)
{
if ($nowWeek != 7)
{
$line .= str_repeat ( '', 7 - $nowWeek );
}
$line .= '';
$html .= $line;

break;
}

$day ++;
}

$html .= <<
星期一星期二星期三星期四星期五星期六星期天
 $day


HTML;
return $html;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621670.htmlTechArticleNote that 32-bit machines have a 2038 problem, so we can still use 32-bit servers in the age range of 1970~2038 DateTime to avoid this problem (so it has nothing to do with 32-bit and 64-bit) Copy the code like...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template