We can find a year is leap year or not in various ways.we can use the following fuctions in php.
- By using date() function
- By Modulus operator
Here is a sample php code that uses modulus(%)operator for finding the leap year.
Write the below code within php tags
Start of php tag
$start = 1000;
$end = 1011;
for($i = $start; $i < $end; $i++)
{
if(($i%4) == 0){$val = "Leap Year";}else{$val = "Not Leap Year";}
echo $i, ' ---> '.$val.'
';
}
End of php tag.
It will produce the output as given below.
1000 ---> Leap Year
1001 ---> Not Leap Year
1002 ---> Not Leap Year
1003 ---> Not Leap Year
1004 ---> Leap Year
1005 ---> Not Leap Year
1006 ---> Not Leap Year
1007 ---> Not Leap Year
1008 ---> Leap Year
1009 ---> Not Leap Year
1010 ---> Not Leap Year
You find leap years for any year ranges by changing the $start and $end.
Labels: Finding leap year or not in php, Php code to find leap year or not
Post a Comment