You need to check the year (not the current) for leap year. Found such an algorithm:

  1. The year is divisible by 400 -> leap -> end
  2. The year is divided by 100 -> not a leap year -> end
  3. The year is divided into 4 -> leap -> end
  4. Year not leap year -> end
  5. Everything

Is he correct? (How can a number be divisible by 400 and not divisible by 100?)

  • 2
    The algorithm is correct, 1 point checks for such years as 1600, 2000 (leap years), and the second - 1700, 1800, 1900 (non-leap) - Valeriy Karchov
  • Thank! And then I broke my head) - oleg_ismaylov
  • Is he correct? (How can a number be divisible by 400 and not divisible by 100?) // Easy. If the number is divisible by 400, it is divisible by 4 and 100. The problem is that after checking the divisibility by 400, we complete the execution, no further checks occur. That is why at first there is a test for 400, then for 100, then for 4, and not vice versa: for economy. A living person will first divide by 4, then by 100, then by 400, losing up to three actions. - knes

2 answers 2

function isLeap($year) { return date("L", mktime(0,0,0, 7,7, $year)); } 

If the year is high, then it returns 1 otherwise 0

UPD:

well or so :) As they say, for lovers of difficult ways :)

 function isLeap($y){return $y % 400 == 1 || ($y % 100 != 0 && ($y & 3) == 0);} 
  • +1 But this is a boring decision. =) We are not looking for easy ways. - knes
  • Small correction: $y % 400 == 0 - BanyRule

Isn't it easier to leave one condition if the remainder of the division by 4 is equal to 0, then the leap year, is non-zero normal?

  • This is done in the third paragraph. - Valeriy Karchov
  • Then the question is as an educational program for me, but why the rest of the points? - Stepan
  • one
    Stepan, 1900 (1100,1300,1400,1500,1700,1800) year is divided by 4, but is not a leap year. So the calendar is arranged. I will not say about the first millennium, but the same time shift, because of which we celebrate the October revolution in November, and the new year twice (old style) is just the compensation of running days during the year. Google it. - knes
  • Thank you honestly did not know))) - Stepan
  • 1100 1300 - this is all the same leap. Read Wikipedia in more detail. And, for example, in Russia, the year 1900 was a leap year, but not in other countries ... so ... - KoVadim