php Calender(日历)代码分享
程序员文章站
2023-12-05 14:22:28
代码如下:
复制代码 代码如下:
<?php
/**
*
* 我的日历
* date_default_timezone_set date mktime
* @param int $year
* @param int $month
* @param string $timezone
* @author fc_lamp
*/
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;
}
代码如下:
复制代码 代码如下:
<?php
/**
*
* 我的日历
* date_default_timezone_set date mktime
* @param int $year
* @param int $month
* @param string $timezone
* @author fc_lamp
*/
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;
}