Skip to content

Core\Time\DateTime::isLeapYear

$dateTime->isLeapYear(): bool

Checks whether the year of a date and time is a leap year.

The result is true when the year has a 29 February, so it has 366 days. In other years the result is false, and the year has 365 days.

A year that divides by 4 is a leap year. A year that divides by 100 is not, unless it also divides by 400. So 2024 and 2000 are leap years, and 1900 and 2100 are not.

Use it when a calculation needs the number of days in the year, for example to split a yearly price into a price per day.

Return value

Returns bool. true in a leap year of the proleptic Gregorian calendar.

Examples

Is this a leap year

<?nvs
// Shows how to check whether a date and time is in a leap year.
var $zone = Core\Time\Zone::of('Europe/Berlin');
// `isLeapYear` returns `true` when the year has a 29 February.
// This prints 2024 is a leap year, and 2025 is not.
array<int> $years = [2024, 2025];
foreach ($years as int $year) {
var $day = Core\Time\DateTime::at(Core\Time\Date::at($year, 6, 1), Core\Time\TimeOfDay::at(0, 0), $zone);
if ($day->isLeapYear()) {
echo $year, " is a leap year\n";
} else {
echo $year, " is not a leap year\n";
}
}
Output
2024 is a leap year
2025 is not a leap year

Century years

<?nvs
// Shows the rule for years that end in 00.
// A year that divides by 4 is a leap year. A year that divides by 100 is not,
// unless it also divides by 400. This prints no, yes and no.
array<int> $years = [1900, 2000, 2100];
foreach ($years as int $year) {
var $day = Core\Time\DateTime::at(Core\Time\Date::at($year, 1, 1), Core\Time\TimeOfDay::at(0, 0), 'UTC');
echo $year, ': ', $day->isLeapYear() ? 'yes' : 'no', "\n";
}
Output
1900: no
2000: yes
2100: no

A yearly price per day

<?nvs
// Shows how to split a yearly subscription price into a price per day.
// The price is in cents, so every result is a whole number.
int $yearlyCents = 3660000;
// A leap year has 366 days and other years have 365. `Core\Math::quotient` divides
// and drops the fraction. This prints 10000 cents per day in 2024 and 10027 in 2025.
array<int> $years = [2024, 2025];
foreach ($years as int $year) {
var $start = Core\Time\DateTime::at(Core\Time\Date::at($year, 1, 1), Core\Time\TimeOfDay::at(0, 0), 'Europe/Paris');
int $days = 365;
if ($start->isLeapYear()) {
$days = 366;
}
echo $year, ': ', Core\Math::quotient($yearlyCents, $days), " cents per day\n";
}
Output
2024: 10000 cents per day
2025: 10027 cents per day