一個簡單實現的日歷,我不知道這段代碼實現的方法有沒有問題,沒有參考前輩,等你理解我的爛代碼之後,再欣賞一下別人的優秀代碼,會更有幫助
01
<html>
02
<head>
03
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf8"
/>
04
<style>
05
#calendardiv,#calendar{width:252px;}
06
#cal_title{height:33px;line-height:33px;text-align:center;overflow:hidden;}
07
#cal_title strong{font-weight:bold;font-size:14px; }
08
#cal_title a{font-weight:bold;font-size:14px;text-decoration:none;}
09
#calendar{border-collapse:collapse;}
10
#calendar td{
11
text-align:center;
12
width:35px;
13
height:20px;
14
line-height:20px;
15
background-color:#efefef;
16
border-bottom:1px solid #fff;
17
border-right:1px solid #fff;
18
}
19
#calendar .even td{background-color:#e6e6e6;}
20
#calendar td .current{display:block;background-color:#f60;color:#fff;}
21
#calendar .current{background-color:#f60!important;color:#fff;}
22
#week td{color:#fff;background-color:#373737;}
23
</style>
24
</head>
25
<body>
26
<?php
27
$date
= isset(
$_GET
[
'd'
]) ?
intval
(
$_GET
[
'd'
]) :
''
;
28
if
(
$date
)
29
{
30
$y
=
substr
(
$date
,0,4);
31
$m
=
substr
(
$date
,4,2);
32
$cur
=
mktime
(0,0,0,
$m
,1,
$y
);
33
}
34
else
35
{
36
$cur
=
mktime
();
37
}
38
39
list(
$year
,
$month
,
$day
) =
explode
(
'-'
,
date
(
'Y-m-d'
,
$cur
));
//年月日
40
$p
=
date
(
'Ym'
,
strtotime
(
'last months'
,
$cur
));
//前一月
41
$n
=
date
(
'Ym'
,
strtotime
(
'next months'
,
$cur
));
//後一月
42
$t
=
date
(
't'
,
$cur
);
//當月多少天
43
$s
=
date
(
'w'
,
mktime
(0,0,0,
$month
,1,
$year
));
//前補空白
44
$e
= 6-(
date
(
'w'
,
mktime
(0,0,0,
$month
,
$t
,
$year
)));
//後補空白
45
?>
46
<div id=
"calendardiv"
>
47
<div id=
"cal_title"
><a href=
"?d=<?=$p?>"
title=
"上一月"
>«</a> <strong><?=
$year
?>年<?=
$month
?>月</strong> <a href=
"?d=<?=$n?>"
title=
"下一月"
>»</a></div>
48
<table id=
"calendar"
>
49
<tr id=
"week"
><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>
50
<?php
51
echo
'<tr class="even">'
;
52
for
(
$i
=0;
$i
<
$s
;
$i
++)
53
{
54
echo
'<td> </td>'
;
55
}
56
for
(
$d
=1;
$d
<=
$t
;
$d
++)
57
{
58
$current
=
$d
==
$day
?
'class="current"'
:
''
;
//當前樣式
59
$r
= (
$d
+
$s
)%7;
//換行
60
61
echo
"<td $current >$d</td>"
;
62
if
(
$r
==0)
63
{
64
echo
'</tr>'
;
65
echo
'<tr class="even">'
;
66
}
67
}
68
for
(
$i
=0;
$i
<
$e
;
$i
++)
69
{
70
echo
'<td> </td>'
;
71
}
72
?>
73
</tr></table></div>