Home  >  Article  >  Backend Development  >  How to calculate the number of days in a given year in php

How to calculate the number of days in a given year in php

PHPz
PHPzOriginal
2023-03-20 16:12:541872browse

PHP is a widely used server-side scripting language that can be used to develop various websites and dynamic applications. Calculating the number of days in a certain year is also a very useful function in certain scenarios. In this article, we will introduce how to calculate the number of days in a given year using PHP.

  1. How to calculate the number of days in a year

To calculate the number of days in a year, you first need to understand that the number of days in a year is uncertain. A year usually has 365 days, but in a leap year, the year has 366 days. A leap year is a year in which the remainder is 0 when divided by 4, but a year that is evenly divisible by 100 but not 400 is not a leap year. Therefore, we can use conditional statements in PHP to determine whether a given year is a leap year and thereby calculate the number of days in a year.

The following is the PHP code:

function countDaysInYear($year) {
  if ($year % 4 == 0) {
    if ($year % 100 == 0 && $year % 400 != 0) {
      return 365;
    } else {
      return 366;
    }
  } else {
    return 365;
  }
}

echo countDaysInYear(2021);  // 输出 365
echo countDaysInYear(2020);  // 输出 366

The countDaysInYear function in the above code accepts a year as a parameter and returns the number of days in the year. Inside the function, we first use the % operator to determine whether the year is a leap year. If the year is divisible by 4, it is a leap year, otherwise it is an ordinary year.

If it is a leap year, we also need to determine whether the year is divisible by 100 but not divisible by 400. If this is the case, it is not a leap year and the number of days is 365; otherwise it is a leap year and the number of days is 366.

  1. How to calculate the day of this year for any day

When we have a specific date, we can also use PHP to calculate the day What day is it this year? This calculation requires the use of the strtotime and date functions in PHP.

Let’s talk about the strtotime function, which can convert text dates (such as "now", "2021-07-12", etc.) into Unix timestamps. The Unix timestamp is the number of seconds from January 1, 1970 00:00:00 UTC to the input date.

We can use the strtotime function to convert the current date into a Unix timestamp, then calculate the time difference from January 1 this year, and finally add 1 to get the date The day of the year.

The following is the PHP code:

function getDayOfYear($date) {
  $now = strtotime($date);
  $start = strtotime(date('Y', $now) .'-01-01');
  return floor(($now - $start) / (60 * 60 * 24)) + 1;
}

echo getDayOfYear('2021-07-12');  // 输出 193

The getDayOfYear function in the above code accepts a date string as a parameter and returns the day of the year that the date is.

Inside the function, we first convert that date string to a Unix timestamp using the strtotime function. Then use the date function to convert January 1st of the current year to a Unix timestamp.

Next, we calculate the time difference between the current date and January 1st, and get the number of days. Because we want to calculate the number of days starting from January 1st, we need to add 1 to this number of days.

  1. Summary

In this article, we introduced how to use PHP to calculate the number of days in a given year and any date in a year The first day of the month. These calculations may not be commonly used in some scenarios, but for programmers, writing such functions can also improve their coding capabilities and increase their toolbox.

The above is the detailed content of How to calculate the number of days in a given year in php. For more information, please follow other related articles on the PHP Chinese website!

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