Home > Backend Development > PHP Problem > How to implement perpetual calendar in php code

How to implement perpetual calendar in php code

藏色散人
Release: 2023-03-14 20:40:01
Original
5181 people have browsed it

How to implement a perpetual calendar with PHP code: 1. Use PHP’s date function to obtain the current year, month and day; 2. Calculate the day of the week corresponding to the first day of the month; 3. Calculate the number of years and the corresponding months of the previous and next months respectively. The number of days can be switched between the previous month and the next month.

How to implement perpetual calendar in php code

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

PHP implements perpetual calendar

To implement a PHP perpetual calendar, first look at the picture:

## The overall implementation logic is: first display the number of days in a certain month in the table. Then calculate the day of the week that the first day of the month corresponds to. Finally, calculate the number of years and days corresponding to the upper and lower months respectively to achieve switching between the upper and lower months. The following will be implemented step by step:

Step 1: Use PHP’s date function to get the current year, month and day:

    Get the current year $y = date("Y");
  • Get the current month$y = date("m");
  • Get the current month$d = date("d");

  • Get How many days are there in the month? $maxday = date("t");

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

  • The next step is the interface code, the code is as shown below

## The first for loop above is used to output the maximum number of days in a loop, and 1-$week indicates the day of the week that the corresponding first day of this month is. The second for loop loops out the date of each row. Among them, $i == $d indicates whether it is the date of this month. If so, the font will be marked in red.
  • The second step is to switch between the previous month and the next month:

Define variables $prey, $preMonth, $nexty, $nextMonth respectively to indicate clicking on the previous month Assign the year and month corresponding to the next month
  • to the upper and lower year and month, and add the link code to the a tag as shown below

## The third step : The year, month and day of initialization for judgment. Determine whether it is the previous or next month of the click. If so, take the year and month. If not, take the present.

$y = isset($_GET["y"])?$_GET["y"]:date("Y");Get the year

    $m = isset ($_GET["m"])?$_GET["m"]:date("m");Get the month
  • if ($y == date("Y") && $m == date("m")) {
  • $d = date("d");
  • } Determine whether it is the current date. The specific code is as shown below

The perpetual calendar is now completed.

Recommended learning: "

PHP Video Tutorial

"


The above is the detailed content of How to implement perpetual calendar in php code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template