PHP Date Time: Checking Leap Years



PHP Date Time Tips - Part 6: Do you want to check if a particular year is a leap year? You can divide the year with 4 or 400. Like this:
<?php
function isLeapYear($year){
  $result = (($year%400 == 0) || ($year%4 == 0 && $year%100 != 0)) ? true : false;
  return $result;
}

echo "2004 is ";
echo isLeapYear(2004) ? "a leap year" : "not leap year";
?>


Series this article:
PHP Date Time: Getting Current
PHP Date Time: Checking Date Validity
PHP Date Time: Formating Timestamp
PHP Date Time: Converting Strings to Timestamps
PHP Date Time: Using English-language time descriptors
PHP Date Time: Checking Leap Years
PHP Date Time: Finding The Number of Days in a Month
PHP Date Time: Finding the Day in Year Number for a Date
PHP Date Time: Finding The Number of Weeks in Year Number for a Date
PHP Date Time: Finding the Day Name for a Date
PHP Date Time: Finding The Total Number of Days in Year
PHP Date Time: Finding The Total Number of Weeks in Year
PHP Date Time: Converting Local Time to GMT
PHP Date Time: Converting Between Different Time Zones
PHP Date Time: Finding the Year Quarter for a Date
PHP Date Time: Converting Minutes to Hours
PHP Date Time: Comparing Dates


Tag: date, leap year Category: PHP Basic Post : March 20th 2008 Read: 2,313 Bookmark and Share

blog comments powered by Disqus