PHP Program to Check Leap Year
A leap year is a calendar year in which an additional day is added to February month. In a leap year, the number of days in February month and the year are 29 and 366 respectively. A year that is not a leap year is called a common year. A year is said to be a leap year if:
- it is divisible by 4.
- it is divisible by 4 but not divisible by 100.
- it is divisible by 4, 100 and 400.
Method 1: Using conditional statements
In the example below, conditional statements are used to identify a leap year.
<?php $year = 2019; if ($year % 400 == 0) { echo $year." is a leap year."; } elseif ($year % 100 == 0) { echo $year." is not a leap year."; } elseif ($year % 4 == 0) { echo $year." is a leap year."; } else { echo $year." is not a leap year."; } ?>
The above code will give the following output:
2019 is not a leap year.
Method 2: Using function
In the example below, a function called leapyear() is created which takes year as argument and prints whether the passed year is a leap year or not.
<?php function leapyear($year) { if ($year % 400 == 0) { echo $year." is a leap year."; } elseif ($year % 100 == 0) { echo $year." is not a leap year."; } elseif ($year % 4 == 0) { echo $year." is a leap year."; } else { echo $year." is not a leap year."; } } leapyear(2019); ?>
The above code will give the following output:
2019 is not a leap year.
Recommended Pages
- PHP - Swap two numbers
- PHP Program - Fibonacci Sequence
- PHP Program - Insertion Sort
- PHP Program - Find Factorial of a Number
- PHP Program - Find HCF of Two Numbers
- PHP Program - Merge Sort
- PHP Program - Shell Sort
- Stack in PHP
- Queue in PHP
- PHP Program - Find LCM of Two Numbers
- PHP Program - To Check Whether a Number is Palindrome or Not
- PHP Program - To Check Whether a String is Palindrome or Not
- PHP Program - Heap Sort
- PHP Program - Quick Sort
- PHP - Swap Two Numbers without using Temporary Variable
- PHP Program - To Check Armstrong Number
- PHP Program - Counting Sort
- PHP Program - Radix Sort
- PHP Program - Find Largest Number among Three Numbers
- PHP Program - Print Floyd's Triangle