Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to check whether a year is bisect (i.e. a leap year)?

Tags:

php

How can I check whether a year is bisect (i.e. a leap year) in php ?

like image 549
Yannis Avatar asked Apr 15 '11 17:04

Yannis


People also ask

How do you check if a year is a leap year in PHP?

php function year_check($my_year){ if ($my_year % 400 == 0) print("It is a leap year"); else if ($my_year % 100 == 0) print("It is not a leap year"); else if ($my_year % 4 == 0) print("It is a leap year"); else print("It is not a leap year"); } $my_year = 1900; year_check($my_year); ?>

How do you determine whether a year is a leap year or not?

To check if a year is a leap year, divide the year by 4. If it is fully divisible by 4, it is a leap year. For example, the year 2016 is divisible 4, so it is a leap year, whereas, 2015 is not. However, Century years like 300, 700, 1900, 2000 need to be divided by 400 to check whether they are leap years or not.

Why are years divisible by 100 not leap years?

By adding one extra day to every fourth year, we get an average of 365.25 days per year, which is fairly close to the actual number. To get even closer to the actual number, every 100 years is not a leap year, but every 400 years is a leap year.

Will 2036 be a leap year?

The complete list of leap years in the first half of the 21st century is therefore 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.


1 Answers

You can use PHP's date() function to do this...

// L will return 1 if it is a leap year, 0 otherwise
echo date('L');

// use your own timestamp
echo date('L', strtotime('last year'));

// for specific year
$year = 1999;
$leap = date('L', mktime(0, 0, 0, 1, 1, $year));
echo $year . ' ' . ($leap ? 'is' : 'is not') . ' a leap year.';

Let me know if this does this trick for you, Cheers!

UPDATE: Added Example for Specific Year

like image 70
McHerbie Avatar answered Oct 13 '22 06:10

McHerbie