Get number of days in Month using PHP

Number of days in month could vary based on month and year value. From https://www.timeanddate.com/calendar/months/

Each month has either 28, 30, or 31 days during a common year, which has 365 days. During leap year, which occur nearly every 4 years, we add an extra (intercalary) day, Leap Day, on 29 February, making leap years 366 days long.

 

Number of days in Month using PHP

 The exact function which returns a correct number of days in month is shown below -

/**
 * Return number of days in month.
 */
public static function daysInMonth($month, $year) {
  if ($month == 2) {
    if (($year % 4 == 0) && ($year % 100 != 0) || ($year % 400 == 0)) {
      return 29;
    }
    return 28;
  }
  return ($month - 1) % 7 % 2 ? 30 : 31;
}

If the month for which the days should be returned is 2(i.e, February), the returned value, in this case, could be either 28 or 29 based on if it is a leap year or not.