How to check whether a year is a leap year
Short answer
The L character in a date format equals 1 for a leap year:
<?php
function isLeapYear(int $year): bool
{
return date('L', mktime(0, 0, 0, 1, 1, $year)) === '1';
}
var_dump(isLeapYear(2024), isLeapYear(2026));Output:
bool(true)
bool(false)Through 29 February
checkdate() checks whether such a date exists:
<?php
var_dump(checkdate(2, 29, 2024), checkdate(2, 29, 2100));Output:
bool(true)
bool(false)The rule itself
A year is a leap year if it is divisible by 4 but not by 100 — or divisible by 400:
<?php
$year = 2000;
var_dump($year % 4 === 0 && ($year % 100 !== 0 || $year % 400 === 0));Output:
bool(true)