search

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";}?>

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

See all articles

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software