Home >Backend Development >PHP Problem >PHP determines whether a year is a leap year

PHP determines whether a year is a leap year

(*-*)浩
(*-*)浩Original
2019-09-27 15:31:167438browse

PHP determines whether a year is a leap year

Determining leap years: (Recommended learning: PHP programming from entry to proficiency)

①. An ordinary year is a leap year if it is divisible by 4 but not divisible by 100. (For example, 2004 is a leap year, and 1900 is not a leap year)

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

Code:

<html>  
<head>
<meta charset="UTF-8" />  
<title>闰年判断</title>  
</head>  
<?php
$year = $_GET["year"];
if (isset($_GET["year"])) {
    if (is_numeric($year)) {
        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 . "不是闰年!";
            }
        }
    } else
        $msg = "请输入正确的格式";
}
?>  
<body>  
<form name=rn method=&#39;get&#39;>  
<b>请输入年:</b>  
<input type="text" name=year>  
<input type="submit" name=sub value="查询">  
<?php
echo $msg;
?>  
</form>    
</body>  
</html>

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