your image

PHP Leap Year Program - javatpoint

javapoint
Related Topic
:- PHP

Leap Year Program

A leap year is the one which has 366 days in a year. A leap year comes after every four years. Hence a leap year is always a multiple of four.

For example, 2016, 2020, 2024, etc are leap years.

Leap Year Program

This program states whether a year is leap year or not from the specified range of years (1991 - 2016).

Example:

 

6. MVC Model & PHP Singleton pattern | Build a CMS using OOP PHP tutorial MVC [2020]

  1. <?php  
  2. function isLeap($year)  
  3. {  
  4.     return (date('L', mktime(0, 0, 0, 1, 1, $year))==1);  
  5. }  
  6. //For testing  
  7. for($year=1991; $year<2016; $year++)  
  8. {  
  9.     If (isLeap($year))  
  10.     {  
  11.         echo "$year : LEAP YEAR<br />\n";  
  12.     }  
  13.     else  
  14.     {  
  15.         echo "$year : Not leap year<br />\n";  
  16.     }  
  17. }  
  18. ?>  

Output:

Leap Year Program in Form

This program states whether a year is leap year or not by inserting a year in the form.

Example:

  1. <html>  
  2. <body>  
  3.     <form method="post">  
  4.         Enter the Year: <input type="text" name="year">  
  5.         <input type="submit" name="submit" value="Submit">  
  6.     </form>  
  7. </body>  
  8. </html>  
  9. <?php   
  10.     if($_POST)  
  11.     {     
  12.         //get the year  
  13.         $year = $_POST['year'];   
  14.         //check if entered value is a number  
  15.         if(!is_numeric($year))  
  16.         {  
  17.             echo "Strings not allowed, Input should be a number";  
  18.             return;  
  19.         }   
  20.         //multiple conditions to check the leap year  
  21.         if( (0 == $year % 4) and (0 != $year % 100) or (0 == $year % 400) )  
  22.         {  
  23.             echo "$year is a Leap Year";    
  24.         }  
  25.         else  
  26.         {  
  27.             echo "$year is not a Leap Year";    
  28.         }  
  29.     }   
  30. ?>  

Output:

On entering year 2016, we get the following output.

On entering year 2019, we get the following output.

Comments