Home  >  Article  >  Backend Development  >  PHP determines whether it is a normal year or a leap year

PHP determines whether it is a normal year or a leap year

(*-*)浩
(*-*)浩Original
2019-10-19 10:46:385073browse

The Gregorian calendar's rules for determining leap years are: there is a leap every four years, there are no leaps every hundred years, and there are leaps every four hundred years.

PHP determines whether it is a normal year or a leap year

The natural way to write it is as follows: (Recommended learning: PHP video tutorial)

$year=2015;
if(($year%4 == 0 && $year%100 != 0) || ($year%400 == 0 )) {
echo "$year is run yerar. 
";
}else {
 
echo "$year is ping year. 
";
}

Method 2: There are two types

(1). An ordinary year that can be divisible by 4 but not divisible by 100 is a leap year. (For example, 2004 is a leap year, and 1900 is not a leap year)

(2). A century year that is divisible by 400 but not divisible by 3200 is a leap year. (For example, the year 2000 is a leap year, and the year 3200 is not a leap year)

Code:

$year=mt_rand(1900,2200);//从1900年到2200,可以自己改,也可以给一个定值。
if($year%100==0){//判断世纪年
if ($year%400==0&&$year%3200!=0){
   echo "世纪年".$year."是闰年!";//世纪年里的闰年
}
else{echo "世纪年".$year."不是闰年!";}
}
else{//剩下的就是普通年了
   if($year%4==0&&$year%100!=0){
   echo "普通年".$year."是闰年!";//普通年里的闰年
}
else {echo "普通年".$year."不是闰年!";}
}

The above is the detailed content of PHP determines whether it is a normal year or a leap 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