PHP Program to find leap year

This PHP program will print the output whether the entered year is a leap year or not.

Three steps are there to find a leap year:

1. year % 4 == 0 -> evenly divisible by 4
2. and year % 100 != 0 -> should not be evenly divisible by 100
3. or year % 400 == 0 -> evenly divisible by 400

if 1st  and 2nd is true  or 3rd is true then it is a leap year.

Here is the entire PHP program to find leap year:

<html>
<body>
	<h2>PHP Script to find Leap year or not - Script by <a href="http://www.tutorialsmade.com/">Tutorialsmade.com</a></h2>
	<form action="" method="post">
		<input type="text" name="year" />
		<input type="submit" />
	</form>
</body>
</html>

<?php 

	if( $_POST )
	{	
		//get the year
		$year = $_POST[ 'year' ];
		
		//check if entered value is a number
		if(!is_numeric($year))
		{
			echo "Strings not allowed, Input should be a number";
			return;
		}
		
		//multiple conditions to check the leap year
		if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )
		{
			echo "$year is a leap year";  
		}
		else
		{
			echo "$year is not a leap year";  
		}

	}
	
?>

Read the comments in the program to understand the flow.

Here is the demo of this program:

demo

 

 

1 thought on “PHP Program to find leap year”

  1. hi can u explain this code
    1. year % 4 == 0 -> evenly divisible by 4
    2. and year % 100 != 0 -> should not be evenly divisible by 100
    3. or year % 400 == 0 -> evenly divisible by 400

    i did not get

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top