How to calculate the total number of days for a given year, month and day through PHP

青灯夜游
Release: 2023-03-12 07:10:01
Original
3429 people have browsed it

In the previous article "PHP array learning, use the bubble algorithm to sort elements in ascending order! ", we introduced the method of using the bubble algorithm to sort array elements in ascending order. This time we will take a look at how to use PHP to calculate the number of a given year, month and day in this year. Interested friends can learn about it~

The theme of this article is implemented through PHP: enter a certain year On a certain day of the month (for example, March 5, 2000), calculate the total number of days so far (it can also be used to determine what day this day is in the year).

Ideological analysis:

If we want to calculate how many days there are as of March 5, we can start from January 1st to March Just add all the 5 days together.

Then we need to find the maximum number of days in the months before March (January and February). To find the maximum number of days in a month, we can use the following function to achieve this function [For a detailed introduction, you can Read the article "How PHP uses functions to calculate the maximum number of days in a given year and month"]

function GetMaxDay($year,$month){
	$time = strtotime("{$year}-{$month}"); //取得一个日期的 Unix 时间戳;
	$date=date("t",$time);
	return $date;
}
Copy after login

Test it: Output the maximum number of days in February 2000

echo GetMaxDay(2000,2);
Copy after login

How to calculate the total number of days for a given year, month and day through PHP

Because 2000 is a leap year, there are 29 days in February, OK.

Then you need to add up these maximum days, which requires using a loop (I use a for loop). The year starts in January, so the initial condition is $i=1; it must be added until the specified month (March), so the restriction condition is $i<$month .

$sum=0;  
for($i=1;$i<$month;$i++){
	$sum += GetMaxDay($year,$i);
}
Copy after login

Test it, calculate all the days before March 2000 (31 29=60), see if it is 60 days:

How to calculate the total number of days for a given year, month and day through PHP

OK, Function implemented!

Finally, add the number of days obtained to $sum and the number of days in the specified day $day of the specified $month. Let’s take a look at the complete code:

Copy after login

Output result:

How to calculate the total number of days for a given year, month and day through PHP

Okay, the code for finding the total number of days is encapsulated into a functional function:

 ";
}
?>
Copy after login

Call this functionGetDays($year,$month,$day)

GetDays(2000,3,5);
GetDays(2001,3,5);
GetDays(2001,2,5);
Copy after login

Let’s take a look at the output:

How to calculate the total number of days for a given year, month and day through PHP

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of How to calculate the total number of days for a given year, month and day through PHP. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!