Jump to content

Finding a leap year?


shadiadiph

Recommended Posts

No, the date() function requires a timestamp - if you don't supply one then it assumes the current date/time. Here is a small function that you can pass a year value to to determine if it is a leap year (also included a loop for testing purposes):

 

<?php

function isLeap($year)
{
    return (date('L', mktime(0, 0, 0, 1, 1, $year))==1);
}

//For testing
for($year=1899; $year<2020; $year++)
{
    If (isLeap($year))
    {
        echo "$year : LEAP YEAR<br />\n";
    }
    else
    {
        echo "$year : Not leap year<br />\n";
    }
}

?>

Link to comment
Share on other sites

eventhough the date() function comes equipped with a leapyear check.. You could just as easily divide the year by 4... and see if it returns a remainder... using the modulus operator.

 

 

if(1776 % 4 == 0) echo "Christopher Columbus sailed the ocean on a leap year!";

Link to comment
Share on other sites

actually that doesnt seem like what i am looking for eg somone enters a year of birth 1900 - current 2010 or more will that work?

 

Yes, that function works for any year that you pass to it and returns a true or false based upon whether that year is a leap year.

 

eventhough the date() function comes equipped with a leapyear check.. You could just as easily divide the year by 4... and see if it returns a remainder... using the modulus operator.

 

 

if(1776 % 4 == 0) echo "Christopher Columbus sailed the ocean on a leap year!";

 

Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year:

($year%4==0 && ($year%100!=0 || $year%400==0))

After some more though on this I was a littel curious and decided to test what the performance difference was between determining leap year via the date() function vs. determining the leap year through mathmatical calculations. Although we are talking about milliseconds, the following functin is almost 100 times faster thant he function I posted above:

  
function isLeap($year)
{
    return ($year%4==0 && ($year%100!=0 || $year%400==0));
}
  

Link to comment
Share on other sites

Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0))

Nice, I did not know this

Link to comment
Share on other sites

Nope, leap year is not determined by whether it is divisable by 4. There is more to it than that. If the year is divisable by 4 AND it is not divisible by 100 (UNLESS it is also divisible by 400) then it is a leap year: ($year%4==0 && ($year%100!=0 || $year%400==0))

Nice, I did not know this

 

Yeah, I'm a bit of a perfectionist. Checking for divisible by 4 will work for any date from 1901 to 2099. So, if we are working with birth dates of people that are currently living they would have to be 110 years old for the rule to fail - pretty unlikely. But, since developers will commonly pick up existing code to reuse for other purposes and/or the OP could be dealing with a geaneology project it's better safe than sorry to do it exactly right.

Link to comment
Share on other sites

just

do this

$remainder=$currentYear MOD 4

if ($remainder==0)

echo 'Leap year';

else

echo 'NOT Leap year';

 

Except 1900 or 2100 are not Leap Years and your code will mark them as such.

Link to comment
Share on other sites

just

do this

$remainder=$currentYear MOD 4

if ($remainder==0)

echo 'Leap year';

else

echo 'NOT Leap year';

 

Yeah, not only did you fail to read the previous posts where a solution was provided (#3 & #7) your solution is flawed - for reasons stated in the previous posts.

Link to comment
Share on other sites

  • 2 years later...

Since we are bumping this (already solved) thread - a solution using mktime/date has already been posted and it's been pointed out that using mktime/date doesn't work for people born before 1970 or 1901 (depending on php version and 32/64 bit operating system.)

 

Topic locked...

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.