PHP设计日历类一 (38),日历38
PHP设计日历类一 (38),日历38
由两个文件组成:
第一个test.php
<style><span> table { border:1px solid #</span><span>050</span><span>; } .fontb { color:white; background:blue; } th { width:30px; } td,th { height:30px; text</span>-<span>align:center; } form { margin:0px; padding:0px; } </span></style> <?<span>php include </span><span>"</span><span>calendar.class.php</span><span>"</span><span>; $calendar</span>=<span>new</span><span> Calendar; $calendar</span>-><span>out</span>();
第二个日历类:calendar.class.php
<?<span>php </span><span>class</span><span> Calendar { </span><span>private</span> $year; <span>//</span><span>当前的年</span> <span>private</span> $month; <span>//</span><span>当前的月</span> <span>private</span> $start_weekday; <span>//</span><span>当月的第一天对应的是周几</span> <span>private</span> $days; <span>//</span><span>当前月一共多少天</span> <span> function __construct(){ $</span><span>this</span>->year=isset($_GET[<span>"</span><span>year</span><span>"</span>]) ? $_GET[<span>"</span><span>year</span><span>"</span>] : date(<span>"</span><span>Y</span><span>"</span><span>); $</span><span>this</span>->month=isset($_GET[<span>"</span><span>month</span><span>"</span>]) ? $_GET[<span>"</span><span>month</span><span>"</span>] : date(<span>"</span><span>m</span><span>"</span><span>); $</span><span>this</span>->start_weekday=date(<span>"</span><span>w</span><span>"</span>, mktime(<span>0</span>, <span>0</span>, <span>0</span>, $<span>this</span>->month, <span>1</span>, $<span>this</span>-><span>year)); $</span><span>this</span>->days=date(<span>"</span><span>t</span><span>"</span>, mktime(<span>0</span>, <span>0</span>, <span>0</span>, $<span>this</span>->month, <span>1</span>, $<span>this</span>-><span>year)); } function </span><span>out</span><span>(){ echo </span><span>'</span><span><table align="center"></span><span>'</span><span>; $</span><span>this</span>->chageDate(<span>"</span><span>test.php</span><span>"</span><span>); $</span><span>this</span>-><span>weeksList(); $</span><span>this</span>-><span>daysList(); echo </span><span>'</span><span></table></span><span>'</span><span>; } </span><span>private</span><span> function weeksList(){ $week</span>=array(<span>'</span><span>日</span><span>'</span>,<span>'</span><span>一</span><span>'</span>,<span>'</span><span>二</span><span>'</span>,<span>'</span><span>三</span><span>'</span>,<span>'</span><span>四</span><span>'</span>,<span>'</span><span>五</span><span>'</span>,<span>'</span><span>六</span><span>'</span><span>); echo </span><span>'</span><span><tr></span><span>'</span><span>; </span><span>for</span>($i=<span>0</span>; $i<count($week); $i++<span>) echo </span><span>'</span><span><th class="fontb"></span><span>'</span>.$week[$i].<span>'</span><span></th></span><span>'</span><span>; echo </span><span>'</span><span></tr></span><span>'</span><span>; } </span><span>private</span><span> function daysList(){ echo </span><span>'</span><span><tr></span><span>'</span><span>; </span><span>//</span><span>输出空格(当前一月第一天前面要空出来)</span> <span>for</span>($j=<span>0</span>; $j<$<span>this</span>->start_weekday; $j++<span>) echo </span><span>'</span><span><td> </td></span><span>'</span><span>; </span><span>for</span>($k=<span>1</span>; $k<=$<span>this</span>->days; $k++<span>){ $j</span>++<span>; </span><span>if</span>($k==date(<span>'</span><span>d</span><span>'</span><span>)) echo </span><span>'</span><span><td class="fontb"></span><span>'</span>.$k.<span>'</span><span></td></span><span>'</span><span>; </span><span>else</span><span> echo </span><span>'</span><span><td></span><span>'</span>.$k.<span>'</span><span></td></span><span>'</span><span>; </span><span>if</span>($j%<span>7</span>==<span>0</span><span>) echo </span><span>'</span><span></tr><tr></span><span>'</span><span>; } </span><span>//</span><span>后面几个空格</span> <span>while</span>($j%<span>7</span>!==<span>0</span><span>){ echo </span><span>'</span><span><td> </td></span><span>'</span><span>; $j</span>++<span>; } echo </span><span>'</span><span></tr></span><span>'</span><span>; } </span><span>private</span><span> function prevYear($year, $month){ $year</span>=$year-<span>1</span><span>; </span><span>if</span>($year < <span>1970</span><span>) $year </span>= <span>1970</span><span>; </span><span>return</span> <span>"</span><span>year={$year}&month={$month}</span><span>"</span><span>; } </span><span>private</span><span> function prevMonth($year, $month){ </span><span>if</span>($month == <span>1</span><span>) { $year </span>= $year -<span>1</span><span>; </span><span>if</span>($year < <span>1970</span><span>) $year </span>= <span>1970</span><span>; $month</span>=<span>12</span><span>; }</span><span>else</span><span>{ $month</span>--<span>; } </span><span>return</span> <span>"</span><span>year={$year}&month={$month}</span><span>"</span><span>; } </span><span>private</span><span> function nextYear($year, $month){ $year </span>= $year + <span>1</span><span>; </span><span>if</span>($year > <span>2038</span><span>) $year </span>= <span>2038</span><span>; </span><span>return</span> <span>"</span><span>year={$year}&month={$month}</span><span>"</span><span>; } </span><span>private</span><span> function nextMonth($year, $month){ </span><span>if</span>($month==<span>12</span><span>){ $year</span>++<span>; </span><span>if</span>($year > <span>2038</span><span>) $year</span>=<span>2038</span><span>; $month</span>=<span>1</span><span>; }</span><span>else</span><span>{ $month</span>++<span>; } </span><span>return</span> <span>"</span><span>year={$year}&month={$month}</span><span>"</span><span>; } </span><span>private</span> function chageDate($url=<span>""</span><span>){ echo </span><span>'</span><span><tr></span><span>'</span><span>; echo </span><span>'</span><span><td><a href="?</span><span>'</span>.$<span>this</span>->prevYear($<span>this</span>->year, $<span>this</span>->month).<span>'</span><span>"></span><span>'</span>.<span>'</span><span><<</span><span>'</span>.<span>'</span><span></a></td></span><span>'</span><span>; echo </span><span>'</span><span><td><a href="?</span><span>'</span>.$<span>this</span>->prevMonth($<span>this</span>->year, $<span>this</span>->month).<span>'</span><span>"></span><span>'</span>.<span>'</span><span><</span><span>'</span>.<span>'</span><span></a></td></span><span>'</span><span>; echo </span><span>'</span><span><td colspan="3"></span><span>'</span><span>; echo </span><span>'</span><span><form></span><span>'</span><span>; echo </span><span>'</span><span><select name="year" onchange="window.location=\'</span><span>'</span>.$url.<span>'</span><span>?year=\'+this.options[selectedIndex].value+\'&month=</span><span>'</span>.$<span>this</span>->month.<span>'</span><span>\'"></span><span>'</span><span>; </span><span>for</span>($sy=<span>1970</span>; $sy <= <span>2038</span>; $sy++<span>){ $selected </span>= ($sy==$<span>this</span>->year) ? <span>"</span><span>selected</span><span>"</span> : <span>""</span><span>; echo </span><span>'</span><span><option </span><span>'</span>.$selected.<span>'</span><span> value="</span><span>'</span>.$sy.<span>'</span><span>"></span><span>'</span>.$sy.<span>'</span><span></option></span><span>'</span><span>; } echo </span><span>'</span><span></select></span><span>'</span><span>; echo </span><span>'</span><span><select name="month" onchange="window.location=\'</span><span>'</span>.$url.<span>'</span><span>?year=</span><span>'</span>.$<span>this</span>->year.<span>'</span><span>&month=\'+this.options[selectedIndex].value"></span><span>'</span><span>; </span><span>for</span>($sm=<span>1</span>; $sm<=<span>12</span>; $sm++<span>){ $selected1 </span>= ($sm==$<span>this</span>->month) ? <span>"</span><span>selected</span><span>"</span> : <span>""</span><span>; echo </span><span>'</span><span><option </span><span>'</span>.$selected1.<span>'</span><span> value="</span><span>'</span>.$sm.<span>'</span><span>"></span><span>'</span>.$sm.<span>'</span><span></option></span><span>'</span><span>; } echo </span><span>'</span><span></select></span><span>'</span><span>; echo </span><span>'</span><span></form></span><span>'</span><span>; echo </span><span>'</span><span></td></span><span>'</span><span>; echo </span><span>'</span><span><td><a href="?</span><span>'</span>.$<span>this</span>->nextYear($<span>this</span>->year, $<span>this</span>->month).<span>'</span><span>"></span><span>'</span>.<span>'</span><span>>></span><span>'</span>.<span>'</span><span></a></td></span><span>'</span><span>; echo </span><span>'</span><span><td><a href="?</span><span>'</span>.$<span>this</span>->nextMonth($<span>this</span>->year, $<span>this</span>->month).<span>'</span><span>"></span><span>'</span>.<span>'</span><span>></span><span>'</span>.<span>'</span><span></a></td></span><span>'</span><span>; echo </span><span>'</span><span></tr></span><span>'</span><span>; } }</span>
效果:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
