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

WBOY
Freigeben: 2016-06-01 11:58:00
Original
1665 Leute haben es durchsucht

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

注意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 = 2038)
  {
   $year = date ( 'Y' );
  }
 } else
 {
  if ($year   {
   $year = date ( 'Y' );
  }

 }

 if ($month 12)
 {
  $month = date ( 'm' );
 }

 //上一年
 $pretYear = $year - 1;
 //上一月
 $mpYear = $year;
 $preMonth = $month - 1;
 if ($preMonth  {
  $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  {
  $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  {
  $year = $nowDate->format( 'Y' );
 }

 if ($month 12)
 {
  $month = $nowDate->format('m' );
 }

 //上一年
 $pretYear = $year - 1;
 //上一月
 $mpYear = $year;
 $preMonth = $month - 1;
 if ($preMonth  {
  $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  {
  $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;
}
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!