一个简单实现的日历,我不知道这段代码实现的方法有没有问题,没有参考前辈,等你理解我的烂代码之后,再欣赏一下别人的优秀代码,会更有帮助
01
02
03
<meta http-equiv="</code">
<code class="string">"Content-Type" content="text/html; charset=utf8" />
04
<style></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
24
25
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
<code class="variable">$t = date('t',$cur); //当月多少天
43
<code class="variable">$s = date('w',mktime(0,0,0,$month,1,$year)); //前补空白
44
<code class="variable">$e = 6-(date('w',mktime(0,0,0,$month,<code class="variable">$t,$year)));//后补空白
45
?>
46
<div id="</code">
<code class="string">"calendardiv">
47
<div id="</code">
<code class="string">"cal_title">"?d==$p?>" title="上一月">« =$year?>年=$month?>月 "?d==$n?>"title="下一月">»
48
<table id="</code">
<code class="string">"calendar">
49
<tr id="</code">
<code class="string">"week">日
一
二
三
四
五
六
50
<?php
51
echo '<tr class="even">'<code class="plain">;
52
for($i=0;$i<code class="variable">$s;$i++)
53
{
54
echo '<td> </td>';
55
}
56
for($d=1;$d<code class="variable">$t;$d++)
57
{
58
$current=$d==$day?'class="current"':'';//当前样式
59
$r = ($d+<code class="variable">$s)%7;//换行
60
61
echo "<td>$d</td>";
62
if($r==0)
63
{
64
echo '';
65
echo '<tr class="even">'<code class="plain">;
66
}
67
}
68
for($i=0;$i<code class="variable">$e;$i++)
69
{
70
echo '<td> </td>';
71
}
72
?>
73
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号