Monday, 13 July 2015

PHP: Function to get the last day of a month

<?php
/**
    Last date of a month of a year
    
    @param[in] $month - Integer. Default = Current Month
    @param[in] $year - Integer. Default = Current Year
    
    @return Last date of the month and year in yyyy-mm-dd format
*/
function last_day($month = '', $year = '')
{
   if (empty($month))
   {
      $month = date('m');
   }
   
   if (empty($year))
   {
      $year = date('Y');
   }
   
   $result = strtotime("{$year}-{$month}-01");
   $result = strtotime('-1 second', strtotime('+1 month', $result));

   return date('Y-m-d', $result);
}
?>

0 comments:

Post a Comment