PHP日历【转】

Jun 23, 2016 pm 02:34 PM

php日历代码,php日历控件,php日历程序

首先说一下这个php日历代码的实现思路,分析下难点在哪里.
PHP下的日历实现主要是依靠PHP强大的时间,日期函数.包括date()和 mktime(),自己现在发现PHP下的最BT的函数莫过于date(),晕~~自己以前一直以为是那堆数据操作和文件管理的函数.实现日历功能,主要 要先弄清楚它的原理,这样写起程序就简单了.

可能一开始会觉得日历很复杂,主要是每个月的日子对应的星期都有变化,还有闰年等。但仔细分析,日历的实现其实不算太复杂.一个日历最基本的肯定是顶部的 星期标识,为了编程方便和同国际习惯同步,我们把周日做为一周的起点,周六做为终点。这个实现比较简单,一个数组用循环输出就可以了。剩下的主要问题就是 日期的显示了。
日期的显示主要要抓住几个问题:
1. 这个月有多少天?
2. 这个月第一天是星期几?
至于一个月有多少周,这个基本不用考虑,因为我们在日历日期显示中对新的一周(即一般要新起一行,可以找个日历先研究一下,总不能31天全部在一行输出 吧)的处理完全不需要用到每月周数,只需要判断某一天是否是周日就可以了,再加上点html的机巧,在是周日的日期显示前先输出一个tr起新行,这样比用 周数判断方便多了.
下面就只剩前两个问题.第一个问题的意义自然不用说了,其实获得这个数据很简单,通过调用date()和mktime()函数就可以实现.可以使用下面的调用:
date("t",mktime(0,0,0,$month,1,$year));
在这里,mktime(0,0,0,$month,$day,$year)前三个0标识小时,分钟和秒,一般较少用到,直接填0,后边三个参数是标识月, 日,年.这个函数返回的是一个UNIX格式的时间戳,这个基本没什么直接意义,需要用date()函数继续处理.date();函数有很多格式化标识,就 是"t"那个...具体可以参考PHP的文档.date("t",mktime(0,0,0,$ month,$day,$year)返回的就是mktime()返回的时间戳标识的一个月的日期数,范围从28-31.注意,这里必须提供一个具体日期, 比如1号.
至于从第二个问题也是十分重要,因为每个月1号的星期数不是都一样的。所以日历输出是在每个月1号之前必须输出空格,保证每个月1号的星期数是真确的(比 如某月1号是星期4则我必须在周日到周三对应位置都输出空格).这个数据也是用date()和mktime()函数获得:
date("w",mktime(0,0,0,$month,1,$year))
这组函数返回的是每月1号的星期数,返回值从0到6依此表示周日到周六.
这样,一个日历所需要的基本数据就齐备了,可以着手实现日历.
首先是获取月份和年份,这个东西用html表单有许多不同的实现方法,我采用了输入框,这样比较简单,然后form的action是$SCRIPT_NAME,即脚本本身.方法是post.
在得到月份和年份后,就可以输出日历主要表格部分了,首先可以用一个数组输出星期数,从Sun.到Sat.然后开始输出全部的日期,这是一个循环:
for($day=1;$day{....}
大括号里包括几个条件判断.首先是获得月份第一天的星期数,通过一个循环,在之前的个星期数对应位置前输出空格.其次一个判断是判断某天是否是周日,如果是的话要输出一个tr换行.

<?phpheader("content-type:text/html;charset=utf-8");?><meta http-equiv="content-type" content="text/html;charset=utf-8"><style>form{    margin:0px;    padding:0px;}td{    text-align:center;    width:80px;}</style><?phpif(!empty($_GET)){    $year = $_GET['year'];    $month = $_GET['month'];}if(empty($year)){    $year = date('Y');}if(empty($month)){    $month = date('m');}$start_weekday = date('w',mktime(0,0,0,$month,1,$year));//echo $start_weekday;$days = date('t',mktime(0,0,0,$month,1,$year));//echo $days;$week = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');$i = 0;$k = 1;$j = 0;echo '<table border = "1">';echo '<tr><td colspan = 7 style = "text-align:center">'.$year.'年'.$month.'月'.'</td></tr>';echo '<tr>';for($i = 0;$i < 7;$i++){    echo '<td>'.$week[$i].'</td>';}echo '</tr>';echo '<tr>';for($j = 0;$j < $start_weekday;$j++){    echo '<td style="color:#FFFFFF">'.$j.'</td>';}while($k <= $days){    if($k == date('d')){        echo '<td style="color:red">'.$k.'</td>';    }else{        echo '<td>'.$k.'</td>';    }    if(($j+1) % 7 == 0){        echo '</tr><tr>';    }    $j++;    $k++;}while($j % 7 != 0){    echo '<td style="color:#FFFFFF">'.$j.'</td>';    $j++;}echo '</tr>';echo '<tr>';echo "<td><a href=?".lastYear($year,$month).">".'<<'.'</a></td>';echo "<td><a href=?".lastMonth($year,$month).">".'<'.'</a></td>';echo '<td colspan = 3 style = "text-align:center">';echo '<form name = "myform" method = "GET">';echo '<select name = year >';for($start_year = 1970;$start_year<2039;$start_year++){ echo '<option value ='. $start_year.'>'.$start_year.'</option>';}echo '</select>'.'年';echo '<select name = month>';for($start_month = 1;$start_month<=12;$start_month++){ echo '<option value = '.$start_month.'>'.$start_month.'</option>';}echo '</select>';echo '月';echo '<input type = "submit" name = "search" value = "查询">';echo '</form>';echo '</td>';echo "<td><a href=?".nextYear($year,$month).">".'>>'.'</a></td>';echo "<td><a href=?".nextMonth($year,$month).">".'>'.'</a></td>';echo '</tr>';echo '</table>';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--; } return "year=$year&month=$month";}function nextYear($year,$month){ $year = $year+1; return "year=$year&month=$month";}function nextMonth($year,$month){ if($month == 12){  $year = $year +1;  $month = 1; }else {  $month++; } return "year=$year&month=$month";}?>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles