Core\Time\Date::isLeapYear
$date->isLeapYear(): boolReturns true when the year of the date is a leap year, and false when it is not.
A leap year has 366 days, because February has a 29th day. A year is a leap year when it divides by 4. A year that divides by 100 is not a leap year, unless it also divides by 400. So 2024 and 2000 are leap years, and 1900 and 2023 are not.
The result depends only on the year. Every date in 2024 returns true.
Good to know: to check a year without a date, build any date in it, such as 1 January.
Return value
Returns bool. true in a leap year of the Gregorian calendar, otherwise false.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\Date::year, Core\Time\Date::month and Core\Time\Date::day return the parts of a date as numbers. Core\Time\Date::weekday returns a Core\Time\Weekday case, and Core\Time\Date::dayOfYear returns the day of the year from 1. Core\Time\Date::isLeapYear checks if the year is a leap year. |
Examples
Check some years
<?nvs// Shows which years are leap years.
array<int> $years = [2023, 2024, 1900, 2000];
// `isLeapYear` returns a `bool`. This prints no, yes, no and yes.foreach ($years as int $year) { var $firstDay = Core\Time\Date::at($year, 1, 1); echo $year, ': ', $firstDay->isLeapYear() ? 'yes' : 'no', "\n";}2023: no2024: yes1900: no2000: yesThe length of february
<?nvs// Shows how to find the number of days in February and in the whole year.
var $date = Core\Time\Date::at(2028, 7, 14);
// 2028 is a leap year. This prints 29 and then 366.echo $date->isLeapYear() ? 29 : 28, "\n";echo $date->isLeapYear() ? 366 : 365, "\n";29366A birthday on 29 february
<?nvs// Shows how to choose the date of a birthday on 29 February in each year.
// In a year that is not a leap year, this program uses 28 February.class Birthday{ public static function in(int $year): Core\Time\Date { if (Core\Time\Date::at($year, 1, 1)->isLeapYear()) { return Core\Time\Date::at($year, 2, 29); } return Core\Time\Date::at($year, 2, 28); }}
// This prints 28 February for 2025 to 2027, and 29 February for 2028.int $year = 2025;while ($year <= 2028) { echo Birthday::in($year)->format('EEEE d MMMM yyyy'), "\n"; $year = $year + 1;}Friday 28 February 2025Saturday 28 February 2026Sunday 28 February 2027Tuesday 29 February 2028