PHP デザイン カレンダー カテゴリ 1 (38)
は 2 つのファイルで構成されます:
最初の test.php
<style><span style="color: #000000;"> table { border:1px solid #</span><span style="color: #800080;">050</span><span style="color: #000000;">; } .fontb { color:white; background:blue; } th { width:30px; } td,th { height:30px; text</span>-<span style="color: #000000;">align:center; } form { margin:0px; padding:0px; }</span></style><?<span style="color: #000000;">php include </span><span style="color: #800000;">"</span><span style="color: #800000;">calendar.class.php</span><span style="color: #800000;">"</span><span style="color: #000000;">; $calendar</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Calendar; $calendar</span>-><span style="color: #0000ff;">out</span>();
2 番目の Calendar クラス: Calendar.class.php
<?<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Calendar { </span><span style="color: #0000ff;">private</span> $year; <span style="color: #008000;">//</span><span style="color: #008000;">当前的年</span> <span style="color: #0000ff;">private</span> $month; <span style="color: #008000;">//</span><span style="color: #008000;">当前的月</span> <span style="color: #0000ff;">private</span> $start_weekday; <span style="color: #008000;">//</span><span style="color: #008000;">当月的第一天对应的是周几</span> <span style="color: #0000ff;">private</span> $days; <span style="color: #008000;">//</span><span style="color: #008000;">当前月一共多少天</span><span style="color: #000000;"> function __construct(){ $</span><span style="color: #0000ff;">this</span>->year=isset($_GET[<span style="color: #800000;">"</span><span style="color: #800000;">year</span><span style="color: #800000;">"</span>]) ? $_GET[<span style="color: #800000;">"</span><span style="color: #800000;">year</span><span style="color: #800000;">"</span>] : date(<span style="color: #800000;">"</span><span style="color: #800000;">Y</span><span style="color: #800000;">"</span><span style="color: #000000;">); $</span><span style="color: #0000ff;">this</span>->month=isset($_GET[<span style="color: #800000;">"</span><span style="color: #800000;">month</span><span style="color: #800000;">"</span>]) ? $_GET[<span style="color: #800000;">"</span><span style="color: #800000;">month</span><span style="color: #800000;">"</span>] : date(<span style="color: #800000;">"</span><span style="color: #800000;">m</span><span style="color: #800000;">"</span><span style="color: #000000;">); $</span><span style="color: #0000ff;">this</span>->start_weekday=date(<span style="color: #800000;">"</span><span style="color: #800000;">w</span><span style="color: #800000;">"</span>, mktime(<span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, $<span style="color: #0000ff;">this</span>->month, <span style="color: #800080;">1</span>, $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">year)); $</span><span style="color: #0000ff;">this</span>->days=date(<span style="color: #800000;">"</span><span style="color: #800000;">t</span><span style="color: #800000;">"</span>, mktime(<span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>, $<span style="color: #0000ff;">this</span>->month, <span style="color: #800080;">1</span>, $<span style="color: #0000ff;">this</span>-><span style="color: #000000;">year)); } function </span><span style="color: #0000ff;">out</span><span style="color: #000000;">(){ echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><table align="center"></span><span style="color: #800000;">'</span><span style="color: #000000;">; $</span><span style="color: #0000ff;">this</span>->chageDate(<span style="color: #800000;">"</span><span style="color: #800000;">test.php</span><span style="color: #800000;">"</span><span style="color: #000000;">); $</span><span style="color: #0000ff;">this</span>-><span style="color: #000000;">weeksList(); $</span><span style="color: #0000ff;">this</span>-><span style="color: #000000;">daysList(); echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></table></span><span style="color: #800000;">'</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function weeksList(){ $week</span>=array(<span style="color: #800000;">'</span><span style="color: #800000;">日</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">一</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">二</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">三</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">四</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">五</span><span style="color: #800000;">'</span>,<span style="color: #800000;">'</span><span style="color: #800000;">六</span><span style="color: #800000;">'</span><span style="color: #000000;">); echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>($i=<span style="color: #800080;">0</span>; $i<count($week); $i++<span style="color: #000000;">) echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><th class="fontb"><span style="color: #800000;">'</span>.$week[$i].<span style="color: #800000;">'</span><span style="color: #800000;"></th></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function daysList(){ echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">输出空格(当前一月第一天前面要空出来)</span> <span style="color: #0000ff;">for</span>($j=<span style="color: #800080;">0</span>; $j<$<span style="color: #0000ff;">this</span>->start_weekday; $j++<span style="color: #000000;">) echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td> </td></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>($k=<span style="color: #800080;">1</span>; $k<=$<span style="color: #0000ff;">this</span>->days; $k++<span style="color: #000000;">){ $j</span>++<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($k==date(<span style="color: #800000;">'</span><span style="color: #800000;">d</span><span style="color: #800000;">'</span><span style="color: #000000;">)) echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td class="fontb"></span><span style="color: #800000;">'</span>.$k.<span style="color: #800000;">'</span><span style="color: #800000;"></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td></span><span style="color: #800000;">'</span>.$k.<span style="color: #800000;">'</span><span style="color: #800000;"></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($j%<span style="color: #800080;">7</span>==<span style="color: #800080;">0</span><span style="color: #000000;">) echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></tr><tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">后面几个空格</span> <span style="color: #0000ff;">while</span>($j%<span style="color: #800080;">7</span>!==<span style="color: #800080;">0</span><span style="color: #000000;">){ echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td> </td></span><span style="color: #800000;">'</span><span style="color: #000000;">; $j</span>++<span style="color: #000000;">; } echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function prevYear($year, $month){ $year</span>=$year-<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($year < <span style="color: #800080;">1970</span><span style="color: #000000;">) $year </span>= <span style="color: #800080;">1970</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">year={$year}&month={$month}</span><span style="color: #800000;">"</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function prevMonth($year, $month){ </span><span style="color: #0000ff;">if</span>($month == <span style="color: #800080;">1</span><span style="color: #000000;">) { $year </span>= $year -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($year < <span style="color: #800080;">1970</span><span style="color: #000000;">) $year </span>= <span style="color: #800080;">1970</span><span style="color: #000000;">; $month</span>=<span style="color: #800080;">12</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ $month</span>--<span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">year={$year}&month={$month}</span><span style="color: #800000;">"</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function nextYear($year, $month){ $year </span>= $year + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($year > <span style="color: #800080;">2038</span><span style="color: #000000;">) $year </span>= <span style="color: #800080;">2038</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">year={$year}&month={$month}</span><span style="color: #800000;">"</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> function nextMonth($year, $month){ </span><span style="color: #0000ff;">if</span>($month==<span style="color: #800080;">12</span><span style="color: #000000;">){ $year</span>++<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>($year > <span style="color: #800080;">2038</span><span style="color: #000000;">) $year</span>=<span style="color: #800080;">2038</span><span style="color: #000000;">; $month</span>=<span style="color: #800080;">1</span><span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ $month</span>++<span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">year={$year}&month={$month}</span><span style="color: #800000;">"</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span> function chageDate($url=<span style="color: #800000;">""</span><span style="color: #000000;">){ echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td><a href="?</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->prevYear($<span style="color: #0000ff;">this</span>->year, $<span style="color: #0000ff;">this</span>->month).<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"><<</span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"></a></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td><a href="?</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->prevMonth($<span style="color: #0000ff;">this</span>->year, $<span style="color: #0000ff;">this</span>->month).<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"><</span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"></a></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td colspan="3"></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><form></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><select name="year" onchange="window.location=\'</span><span style="color: #800000;">'</span>.$url.<span style="color: #800000;">'</span><span style="color: #800000;">?year=\'+this.options[selectedIndex].value+\'&month=</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->month.<span style="color: #800000;">'</span><span style="color: #800000;">\'"></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>($sy=<span style="color: #800080;">1970</span>; $sy <= <span style="color: #800080;">2038</span>; $sy++<span style="color: #000000;">){ $selected </span>= ($sy==$<span style="color: #0000ff;">this</span>->year) ? <span style="color: #800000;">"</span><span style="color: #800000;">selected</span><span style="color: #800000;">"</span> : <span style="color: #800000;">""</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><option </span><span style="color: #800000;">'</span>.$selected.<span style="color: #800000;">'</span><span style="color: #800000;"> value="</span><span style="color: #800000;">'</span>.$sy.<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.$sy.<span style="color: #800000;">'</span><span style="color: #800000;"></option></span><span style="color: #800000;">'</span><span style="color: #000000;">; } echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></select></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><select name="month" onchange="window.location=\'</span><span style="color: #800000;">'</span>.$url.<span style="color: #800000;">'</span><span style="color: #800000;">?year=</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->year.<span style="color: #800000;">'</span><span style="color: #800000;">&month=\'+this.options[selectedIndex].value"></span><span style="color: #800000;">'</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>($sm=<span style="color: #800080;">1</span>; $sm<=<span style="color: #800080;">12</span>; $sm++<span style="color: #000000;">){ $selected1 </span>= ($sm==$<span style="color: #0000ff;">this</span>->month) ? <span style="color: #800000;">"</span><span style="color: #800000;">selected</span><span style="color: #800000;">"</span> : <span style="color: #800000;">""</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><option </span><span style="color: #800000;">'</span>.$selected1.<span style="color: #800000;">'</span><span style="color: #800000;"> value="</span><span style="color: #800000;">'</span>.$sm.<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.$sm.<span style="color: #800000;">'</span><span style="color: #800000;"></option></span><span style="color: #800000;">'</span><span style="color: #000000;">; } echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></select></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></form></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td><a href="?</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->nextYear($<span style="color: #0000ff;">this</span>->year, $<span style="color: #0000ff;">this</span>->month).<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;">>></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"></a></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"><td><a href="?</span><span style="color: #800000;">'</span>.$<span style="color: #0000ff;">this</span>->nextMonth($<span style="color: #0000ff;">this</span>->year, $<span style="color: #0000ff;">this</span>->month).<span style="color: #800000;">'</span><span style="color: #800000;">"></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;">></span><span style="color: #800000;">'</span>.<span style="color: #800000;">'</span><span style="color: #800000;"></a></td></span><span style="color: #800000;">'</span><span style="color: #000000;">; echo </span><span style="color: #800000;">'</span><span style="color: #800000;"></tr></span><span style="color: #800000;">'</span><span style="color: #000000;">; } }</span></span></span></span></span></span></span>
効果: