php calender(달력)두 버 전 코드 예시(2038 문제 해결)
8870 단어 calender2038 문제 해결일력
우 리 는 또한 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 = <<<HTML
<table width="500" border="1">
<tr align="center">
<td><a href="?y=$pretYear"> </a></td>
<td><a href="?y=$mpYear&m=$preMonth"> </a></td>
<td><a href="?"> </a></td>
<td><a href="?y=$mnYear&m=$nextMonth"> </a></td>
<td><a href="?y=$nextYear"> </a></td>
</tr>
<tr align="center">
<td colspan="5">{$year} {$month} </td>
</tr>
<tr>
<td colspan="5">
<table width="100%" border="1">
<tr align="center">
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#F60;color:#fff;font-weight: bold;"> </td>
<td style="background-color:#F60;color:#fff;font-weight: bold;"> </td>
</tr>
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 = '<tr align="center">';
$line .= str_repeat ( '<td> </td>', $nowWeek - 1 );
}
if ($cday == $currentDay)
{
$style = 'style="color:red;"';
} else
{
$style = '';
}
$line .= "<td $style>$day</td>";
//
if ($nowWeek == 7)
{
$line .= '</tr>';
$html .= $line;
$line = '<tr align="center">';
}
//
if ($day == $lastday)
{
if ($nowWeek != 7)
{
$line .= str_repeat ( '<td> </td>', 7 - $nowWeek );
}
$line .= '</tr>';
$html .= $line;
break;
}
$day ++;
}
$html .= <<<HTML
</table>
</td>
</tr>
</table>
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 = '', $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 = <<<HTML
<table width="500" border="1">
<tr align="center">
<td><a href="?y=$pretYear"> </a></td>
<td><a href="?y=$mpYear&m=$preMonth"> </a></td>
<td><a href="?"> </a></td>
<td><a href="?y=$mnYear&m=$nextMonth"> </a></td>
<td><a href="?y=$nextYear"> </a></td>
</tr>
<tr align="center">
<td colspan="5">{$year} {$month} </td>
</tr>
<tr>
<td colspan="5">
<table width="100%" border="1">
<tr align="center">
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#DAF0DD;"> </td>
<td style="background-color:#F60;color:#fff;font-weight: bold;"> </td>
<td style="background-color:#F60;color:#fff;font-weight: bold;"> </td>
</tr>
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 = '<tr align="center">';
$line .= str_repeat ( '<td> </td>', $nowWeek - 1 );
}
if ($cday == $currentDay)
{
$style = 'style="color:red;"';
} else
{
$style = '';
}
$line .= "<td $style>$day</td>";
//
if ($nowWeek == 7)
{
$line .= '</tr>';
$html .= $line;
$line = '<tr align="center">';
}
//
if ($day == $lastday)
{
if ($nowWeek != 7)
{
$line .= str_repeat ( '<td> </td>', 7 - $nowWeek );
}
$line .= '</tr>';
$html .= $line;
break;
}
$day ++;
}
$html .= <<<HTML
</table>
</td>
</tr>
</table>
HTML;
return $html;
}