Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Perpetual Calendar with php Code

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "how to realize the perpetual calendar with php code". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to realize the perpetual calendar with php code" can help you solve the problem.

Php code to achieve the perpetual calendar method: 1, use the date function of PHP to obtain the current year, month and day; 2, calculate the first day of the month corresponding to the week; 3, respectively calculate the number of years and days corresponding to the next month, to achieve the switch between the previous month and the next month.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

Realization of perpetual calendar by PHP

To implement a PHP perpetual calendar, the picture above:

The overall implementation logic is to display the number of days of a month in table first. Then calculate the day of the week corresponding to the first day of the month. Finally, the corresponding number of years and days of the next month are calculated respectively, and the switch between the previous month and the next month is realized. The following will be implemented step by step:

Step 1: use the date function of PHP to obtain the current year, month and day:

Get current year $y = date ("Y")

Get current month $y = date ("m")

Get the current number $d = date ("d")

Get the number of days in the month $maxday = date ("t")

Get the week date ("w", strtotime ("{$y}-{$m}-1") corresponding to the first day of this month)

Next is the interface code, which is shown in the following figure

The first for loop above is used to output the maximum number of days, and 1-$week represents the corresponding day of the week on the first day of the month. The second for loop, which loops out the date of each line. Where $I = = $d indicates whether it is the date of this month's number, and if so, the font is marked in red.

The second step is to switch between last month and next month:

Define the variable $prey,$preMonth,$nexty,$nextMonth to represent the year and month corresponding to the previous month and the next month.

Assign a value to the following year and month, and add the link code to the a tag as shown below

The third step: initialize the year, month and day, make a judgment. Determine whether it is the previous month or the next month of the click. If it is this, take the year and the month. If not, take the present.

$y = isset ($_ GET ["y"])? $_ GET ["y"]: date ("Y")

M = isset ($_ GET ["m"])? $_ GET ["m"]: date ("m"); month

If ($y = = date ("Y") & & $m = = date ("m")) {

$d = date ("d")

} determines whether it is the current date. The specific code is shown in the following figure

At this point, the perpetual calendar is complete.

This is the end of the introduction on "how to implement the perpetual calendar with php code". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report