Home  >  Article  >  Backend Development  >  php gets how many days in a year

php gets how many days in a year

angryTom
angryTomOriginal
2019-10-30 10:00:424320browse

php gets how many days in a year

php gets how many days in a year

php has no built-in function method for us to use, so we can write A function method to get the number of days in a year and distinguish between leap years and flat years. PHP has a built-in number of days in the month. We can get the number of days in a year by adding the number of days in the month.

The code is as follows:

<?php
function cal_days_in_year($year){
    $days = 0;
    for($month=1;$month<=12;$month++){
        $days = $days + cal_days_in_month(CAL_GREGORIAN,$month,$year);
     }
return $days;
}
 
//闰年
echo "这是闰年一年有:".cal_days_in_year(2000)."天";
echo "<br>;
//平年
echo "这是平年一年有:".cal_days_in_year(1999)."天";
echo "<br>;
//2019年
echo "今年2019年有:".cal_days_in_year(date(&#39;Y&#39;,time()))."天";
 
echo "<br>;

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of php gets how many days in a year. 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