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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles