PHP : How to get Number of days in a month

In this tutorial learn how to get number of days in month using function, pass parameter month and year in function only



Tutorial

<?php
    
    /**
  * Number of days in a month
  *
  * Takes a month/year as input and returns the number of days
  * for the given month/year. Takes leap years into consideration.
  *
  * @param int a numeric month
  * @param int a numeric year
  * @return int
  */
 function tot_days_in_month($month = 0, $year = '')
 {
  if ($month < 1 OR $month > 12)
  {
   return 0;
  }
  elseif ( ! is_numeric($year) OR strlen($year) !== 4)
  {
   $year = date('Y');
  }

  if (defined('CAL_GREGORIAN'))
  {
   return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  }

  if ($year >= 1970)
  {
   return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
  }

  if ($month == 2)
  {
   if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
   {
    return 29;
   }
  }

  $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  return $days_in_month[$month - 1];
 }

 echo tot_days_in_month(2,2020);
?>
ABOVE TUTORIAL OUTPUT :

29

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...