How can I check whether a year is bisect (i.e. 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); ?>
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With