Program to check Leap Year in PHP

 PHP Program to check Leap Year…


Program to check given year is a leap or not:

check Leap Year: Hi, readers here in this tutorial we explain that how we can check whether the given year is a leap year or not using PHP.

Here we start our topic by the logic behind check Leap Year in PHP.

The logic to check Leap Year:

A Leap year is exactly divisible by 4, e.g. 2008,2012, etc.

but some year which is exactly divisible by 4 may not be a leap year e.g. 1900,1800, etc (this case generally arises for century year).

To check Century year is a leap year then the year is only exactly divisible by 400 e.g,2100,2200, etc.

Simple Example:

Here in this example to check Leap Year Program in PHP, we used the modulus operator(%) to check whether the given number is divisible by 4 but not divisible by 100 or exactly divisible by 400.

For understanding this example one must have the prior knowledge of if..else structure in Php and PHP arithmetic and logical operators. 

In this program first of all the number entered by user is stored in the variable and then using if ..else construct to check each condition of leap year in one expression using if statement i.e. if condition evaluates to “true” then print leap year else if evaluates to “false” then print not a leap year .

Full Source Code:

<!Doctype html>
<html>
<head><title>Check Leap Year in PHP</title>
<!-- meta seo-->
</head>
<body>
<!--form is created using post method and submiting values itself -->

<form action="" method="post">
Enter a number:
<input type="text" name="year" />
<input type="submit" value="Check Leap Year" />
</form>
<!--end of form-->

<!--php code starts-->
<?php

// if ($_POST) is true i.e.,checking that the HTML form is submited by the method="post" or not.
 if($_POST){
 
/* collecting the value of input field using $_POST['parameter'] 
 and storing it in the variable. */ 

 $year=$_POST['year'];
 
 /*checking condition of leap year :-
 1.)year is divisible by 4 and not divisible by 100.
 2.)year is divisible by 400 if it is century year.
 */
 if(((($year%4)==0) && (($year%100)!=0))|| (($year%400)==0))
 
 echo "Given year: $year is a Leap Year";
 else
 echo "Give year: $year is not a leap Year";
}

?>
<!--php code ends-->

</body>
</html>



OUTPUT:

 

check Leap Year


 I hope You would like this topic to check Leap Year using PHP

Leave a Comment