作者:神秘网友 发布时间:2020-09-16 09:55:07




获取日历变量 ( Getting Calendar Variables )


 gilaxia/Getty Images


PHP calendars can be useful. You can do things as simple as showing the date, and as complex as setting up an online booking system. This article shows how to generate a simple PHP calendar. When you understand how to do this, you will be able to apply the same concepts to complex calendars you may need.

PHP日历可能很有用。 您可以像显示日期一样简单,也可以像建立在线预订系统一样复杂。 本文介绍了如何生成简单PHP日历。 当您了解如何执行此操作后,便可以将相同的概念应用于您可能需要的复杂日历。

The first part of the code sets some variables that are needed later in the script. The first step is to find out what the current date is using the time () function. Then, you can use the date () function to format the date appropriately for the $day, $month and $year variables. Finally, the code generates the name of the month, which is the title

一周中的日子 ( Days of the Week )

Here you take a close look at the days of the month and prepare to make the calendar table. The first thing is to determine on which day of the week the first of the month falls. With that knowledge, you use the switch () function to determine how many blank days are needed in a calendar before the first day.

在这里,您仔细查看当月的日子并准备制作日历表。 首先是确定每月的第一天是在星期几。 有了这些知识,您就可以使用switch()函数来确定日历中第一天之前需要多少空白天。

Next, count the total days of the month. When you know how many blank days are needed and how many total days are in the month, the calendar can be generated.

接下来,计算每月的总天数。 当您知道需要多少个空白天以及一个月中总共有多少天时,便可以生成日历。

标题和空白日历日 ( Headings and Blank Calendar Days )

The first part of this code echoes the table tags, the month name and the headings for the days of the week. Then it starts a while loop that echoes empty table details, one for each blank day to count down. When the blank days are done, it stops. At the same time, the $day_count is going up by 1 each time through the loop. This keeps count to prevent putting more than seven days in a week.

该代码的第一部分回显表标记,月份名称和一周中各天的标题。 然后,它启动一个while循环 ,该循环回显空的表详细信息,每个空白天向下计数一次。 空白的日子结束后,它停止了。 同时,循环中$ day_count每次增加1。 这样可以保持计数,以防止一周内放置超过7天。

一个月中的天数 ( Days of the Month )

Another while loop fills in the days of the month, but this time it counts up to the last day of the month. Each cycle echoes a table detail with the day of the month, and it repeats until it reaches the last day of the month.

另一个while循环填充该月的日子,但是这次计数到该月的最后一天。 每个周期都与月的某天回显一个表的详细信息,并重复直到到达该月的最后一天。

The loop also contains a conditional statement. This checks if the days of the week have reached 7—the end of the week. If it has, it starts a new row and resets the counter back to 1.

该循环还包含一个条件语句 。 这将检查星期几是否达到7点(星期几)。 如果有,它将开始新的一行并将计数器重置为1。

完成日历 ( Finishing the Calendar )

One last while loop finishes the calendar. This one fills in the rest of the calendar with blank table details if needed. Then the table is closed and the script is complete.

最后一个while循环完成日历。 如果需要,此表将用空白表详细信息填充日历的其余部分。 然后,关闭表并完成脚本。




  1. 试题 算法提高 任意年月日历输出(Java)
  2. ThinkPHP自动加载Loader源码解析
  3. 源码编译安装memcached和PHP扩展memcache整理总结
  4. Javascript漂流记(上)__Date对象、实现日历插件
  5. 按键精灵实现qq群自动签到
  6. PHP+MySql+PDO小案例—文章管理系统
  7. PHP入门---(二)
  8. php + ajax + mysql + echarts实现动态图表绘制