Q:

Leap Year Program in Php

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Leap Year Program

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

Example:

<?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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now