使用PHP生成日历表,完全代码如下:
//filename: calengar.php
header("content-type:text/html;charset=gb2312");
// 设置字符集
?>
form{
margin:0px;
padding:0px;
}
td{
text-align:center;
width:80px;
}
if (!empty($_GET)) {
$year = $_GET['year'];
$month = $_GET['month'];
}
if (empty($year)) {
$year = date('Y');
}
if (empty($month)) {
$month = date('m');
}
$day = date('d');
$start_weekday = date('w',mktime(0,0,0,$month,1,$year));
$days = date('t',mktime(0,0,0,$month,1,$year));
//echo $year.'
';
//echo $month.'
';
//echo $day.'
';
//echo $days.'
';
//echo $start_weekday;
echo '';echo ''.$year.'年'.$month.'月'.' | ';echo '';$week = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
$i = 0; //打印出来周日到周一
for ($i=0;$i<7;$i++){
echo '
'.$week[$i].' | ';}
echo '
'; echo '';$j = 0;
for ($j=0;$j<$start_weekday;$j++) {
echo '
'.$j.' | '; //打印空白,#FFFFFF为透明色,把前几天省去}
$k = 1;
while ($k <= $days){ //打印日历
if ($k == $day){
echo '
'.$k.' | '; } else {
echo '
'.$k.' | '; }
if (($j+1)%7 == 0) {
echo '
'; }
$j++;
$k++;
}
while ($j % 7 != 0) {
echo '
'.$j.' | '; $j++;
}
echo '
';
function lastyear($year,$month) {
$year = $year-1;
return "year=$year&month=$month";
}
function lastmonth($year,$month) {
if ($month == 1) {
$year = $year -1;
$month = 12;
} else {
$month -= 1;
}
return "year=$year&month=$month";
}
function nextyear($year,$month){
$year += 1;
return "year=$year&month=$month";
}
function nextmonth($year,$month){
if ($month == 12){
$year += 1;
$month = 1;
}else{
$month +=1;
}
return "year=$year&month=$month";
}
echo '';echo ''; echo ' echo ' for ($start_year=1970;$start_year<2049;$start_year++) { echo ''; } echo ''.'年'; echo ' for($start_month = 1;$start_month<=12;$start_month++){ echo ''; } echo ''; echo '月'; echo ''; echo ''; echo ' | ';
echo '
'; echo '
'; ?>
显示效果如下:
2011年07月 |
星期日 | 星期一 | 星期二 | 星期三 | 星期四 | 星期五 | 星期六 |
0 | 1 | 2 | 3 | 4 | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 36 | 37 | 38 | 39 | 40 | 41 |
| | | | |
阅读(4716) | 评论(2) | 转发(0) |