PHP Leap Year Program - javatpoint
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]
- <?php
- function isLeap($year)
- {
- return (date('L', mktime(0, 0, 0, 1, 1, $year))==1);
- }
- //For testing
- for($year=1991; $year<2016; $year++)
- {
- If (isLeap($year))
- {
- echo "$year : LEAP YEAR<br />\n";
- }
- else
- {
- echo "$year : Not leap year<br />\n";
- }
- }
- ?>
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:
- <html>
- <body>
- <form method="post">
- Enter the Year: <input type="text" name="year">
- <input type="submit" name="submit" value="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";
- }
- }
- ?>
Output:
On entering year 2016, we get the following output.
On entering year 2019, we get the following output.