Get last date or total number of days in PHP

Do you want to find out the last date of the month in PHP. Here is a simple way by providing the valid date. It then takes the month from the date provided and returns the result.

1] Example to find out last date from the Month of the date parsed

     $date = '2016-05-05';
     echo date('Y-m-t', strtotime($date));

     //output:
     2016-05-31

Continue reading “Get last date or total number of days in PHP”

List all months and year within date range in PHP

The function to get all months with their year which falls under the date range specified (i.e. your start date and end date).
It uses PHP DateTime Class to get the result and tested with PHP 5.3

     function _daterange_months($start_date, $end_date, $format = 'm-Y')
     {
          $begin= new DateTime(date('Y-m-01', strtotime($start_date)));
          $end	= new DateTime(date('Y-m-t', strtotime($end_date)));
          $end	= $end->modify( '+1 day' );

          $result	= array();
          $daterange	= new DatePeriod($begin, new DateInterval('P1M'), $end);
          foreach($daterange as $date)
          {
               $result[] = $date->format($format);
          }
          return $result;
     }

Continue reading “List all months and year within date range in PHP”

List all dates within date range in PHP

The function to get all dates which falls under the date range specified (i.e. your start date and end date).
It uses PHP DateTime Class to get the result and tested with PHP 5.3

function _daterange_days($start_date, $end_date, $format = 'Ymd')
{
     $begin = new DateTime($start_date);
     $end = new DateTime($end_date);
     $end = $end->modify( '+1 day' );

     $result = array();
     $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
     foreach($daterange as $date)
     {
          $result[] = $date->format($format);
     }
     return $result;
}

Continue reading “List all dates within date range in PHP”