PHP Calender (Calendar) Two version code examples

Source: Internet
Author: User
Tags abs datetime

A simple PHP calender (calendar) solves 2038 problems, so it can be used on both 32-bit and 64-bit machines, the code is simple and easy to modify

  Note 32-bit machine has 2038 problems, so 32-bit server life span 1970 ~2038 years   We can also use DateTime to circumvent this problem (this is not related to 32-bit 64-bit)   code as follows: <?php/** & nbsp;*   * My calendar  * 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));    //is a 32-bit machine  if (IS32 ())  {  if ($year < 1970 or $year >= 2038)   {    $ye AR = Date (' Y ');  } &nbsp Else  {  if ($year <= 0)   {    $year = date (' Y ');  }     }    if ($month <= 0 or $month >)  {  $month = date (' m ');      //previous year &nbs p; $pretYear = $year-1;  //month   $mpYear = $year; &nbsp. $preMonth = $month-1;  if ($preMonth <= 0)  {  $preMonth = 1   $mpYear = $pretYear;      //next year   $n Extyear = $year + 1;  //next month   $mnYear = $year;   $nextMonth = $month + 1;  if ($nextMonth >)  {  $nextMonth = 1   $mnYear = $nextYear; &nbsp}    //Calendar header   $html = <<<html <table width= "$" border= "1" >   <tr align= "center" >     <TD>&L T;a href= "? y= $pretYear" > Last year </a></td>     <td><a href= "? y= $mpYear &m= $preMonth" > Last month </a></td>      <td><a href= "?" > Back to today </a></td>     <td><a href= "? y= $mnYear &m= $nextMonth" > next month </a> </td>  <td><a href= "? y= $nextYear" > next year </a></td>   </tr>   <TR align= "center" >     <TD colspan= "5" >{$year} year {$month} month </td>   </tr> &NBsp <tr>    <TD colspan= "5" >   <table width= "100%" border= "1" >    <TR align= " Center ">     <TD style=" Background-color: #DAF0DD; " > Monday </td>     <TD style= "Background-color: #DAF0DD;" > Tuesday </td>     <TD style= "Background-color: #DAF0DD;" > Wednesday </td>     <TD style= "Background-color: #DAF0DD;" > Thursday </td>     <TD style= "Background-color: #DAF0DD;" > Friday </td>     <TD style= "background-color: #F60; color: #fff; font-weight:bold;" > Saturday </td>     <TD style= "background-color: #F60; color: #fff; font-weight:bold;" > Sunday </td>    </tr> HTML;     $currentDay = date (' Y-m-j ');    //The last day of the month   $lastday = date (' J ', Mktime (0, 0, 0, $nextMonth, 0, $year));    //cycle output days   $day = 1;   $line = ';  while ($day <= $lastday)  {  $cday = $year. '-' . $Month. '-' . $day;    //current day of the week   $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>";    //week end   if ($nowWeek = = 7)   {    $line. = ' </tr> ';     $html. = $line;     $line = ' <tr align= ' center ' > ';  }    //All month end   if ($day = = $lastday)   {   if ($nowWeek!= 7)    {    $line. = Str_repeat (' <td> </td> ', 7-$nowWeek);   &nbsp     $line. = ' </tr> ';     $html. = $line;      break;  }     $day++; &NBSP}     $html. = <<<html   </table>   </td>   </tr> </ Table> HTML;  return $html; }  /**  *   * detection is a 32-bit machine  * @author fc_lamp  * @blog: fc-lamp.blog.163.com  */function Is32 () {  $is = false;  if (Strtotime (' 2039-10-10 ') = False)  {  $is = True;  }   return $is 32;       Use the DateTime class to resolve 2038 issues, so that no 32 digits and 64 bits, the code is as follows:     code is as follows: <?php/**  *   * My Calendar (D Atetime 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 = ', $mo Nth = ', $timezone = ' Asia/shanghai ') {   date_default_timezone_set ($timezone);   $year = ABS (intval ($year));   $month = ABS (Intval ($month));     $nowDate = new DateTime (); &nBsp  if ($year <= 0)  {  $year = $nowDate->format (' Y ');  }    if ($month <= 0 or $mo Nth >)  {  $month = $nowDate->format (' m ');  }    //previous year   $pretYear = $year-1;  //month   $mpYear = $year;   $preMonth = $month-1;  if ($preMonth <= 0)  {  $preMonth = 1   $mpYear = $pretYear;      //next year   $n Extyear = $year + 1;  //next month   $mnYear = $year;   $nextMonth = $month + 1;  if ($nextMonth >)  {  $nextMonth = 1   $mnYear = $nextYear; &nbsp}    //Calendar header   $html = <<<html <table width= "$" border= "1" >   <tr align= "center" >     <TD>&L T;a href= "? y= $pretYear" > Last year </a></td>     <td><a href= "? y= $mpYear &m= $preMonth" > Last month </a></td>      <td><a href= "?" > Back to today </a></td>     <Td><a href= "? y= $mnYear &m= $nextMonth" > next month </a></td>  <td><a href= "? y=$ Nextyear "> next year </a></td>   </tr>   <tr align=" center ">     <TD colspan= "5" >{$year} year {$month} month </td>   </tr>   <tr>    <TD colspan= "5" >   <table width= "100%" border= "1" >    <tr align= "center" >     <TD style= " Background-color: #DAF0DD; " > Monday </td>     <TD style= "Background-color: #DAF0DD;" > Tuesday </td>     <TD style= "Background-color: #DAF0DD;" > Wednesday </td>     <TD style= "Background-color: #DAF0DD;" > Thursday </td>     <TD style= "Background-color: #DAF0DD;" > Friday </td>     <TD style= "background-color: #F60; color: #fff; font-weight:bold;" > Saturday </td>     <TD style= "background-color: #F60; color: #fff; font-weight:bold;" > Sunday </td>    </Tr> HTML;     $currentDay = $nowDate->format (' y-m-j ');    //The last day of the month   $creatDate = new DateTime ("$year-$nextMonth-0");   $lastday = $creatDate->format (' J ');   $creatDate = NULL;    //cycle output days   $day = 1;   $line = ';  while ($day <= $lastday)  {  $cday = $year. '-' . $month. '-' . $day;    //current day of the week   $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>";    //week end   if ($nowWeek = = 7)   {    $line. = ' </tr> ';     $html. = $line;     $line = ' <tr align= ' center ' > ';  }    //All month end   if ($day = = $lastday)   {   if ($nowWeek!= 7)    {    $line. = Str_repeat (' <td> </td> ', 7-$nowWeek);   &nbsp     $line. = ' </tr> ';     $html. = $line;      break;  }     $day + +; &NBSP}     $html. = <<<html   </table>   </td>   </tr> </ Table> HTML;  return $html; }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.