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;


	$range_months = _daterange_months('2016-03-31', '2016-06-02', 'M-Y');


array(4) {
  string(8) "Mar-2016"
  string(8) "Apr-2016"
  string(8) "May-2016"
  string(8) "Jun-2016"