PHP program to check if a year is leap year or not

PHPServer Side ProgrammingProgramming

To check if a year is leap year or not in PHP, the code is as follows −

Example

 Live Demo

<?php
function year_check($my_year)
{
   if ($my_year % 400 == 0)
      print("It is a leap year");
   if ($my_year % 4 == 0)
      print("It is a leap year");
   else if ($my_year % 100 == 0)
      print("It is not a leap year");
   else
      print("It is not a leap year");
}
$my_year = 2024;
year_check($my_year);
?>

Output

It is a leap year

A function named ‘year_check’ is defined that takes a year as a parameter. It checks to see if the year can be divided by 400 or 4 completely, if yes, it means it is a leap year. Otherwise, it is a leap year. The value for year is defined outside the function, and the function is called by passing this year as a parameter to it. Relevant output is displayed on the console.

raja
Published on 02-Jul-2020 10:26:28
Advertisements