Home >Backend Development >PHP Problem >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('Y',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!