Skip to content

Instantly share code, notes, and snippets.

@plasticbrain
Created June 6, 2013 18:31
Show Gist options
  • Select an option

  • Save plasticbrain/5723781 to your computer and use it in GitHub Desktop.

Select an option

Save plasticbrain/5723781 to your computer and use it in GitHub Desktop.
PHP - Get months in date range
function get_months_in_range($start, $end, $format='timestamp', $create_empty_array=false) {
$i = $formatted_i = $start = strtotime($start);
if( $format != 'timestamp' ) $formatted_i = date($format, $i);
$end = strtotime($end);
$dates = $create_empty_array ? array($formatted_i => array()) : array($formatted_i);
while($i < $end) {
$month = $formatted_day = mktime(0, 0, 0, date('m', $i)+1, date('d',$i), date('Y', $i));
if( $format != 'timestamp' ) {
$formatted_month = date($format, $month);
}
if( $create_empty_array ) {
$dates[$formatted_month] = array();
} else {
$dates[] = $formatted_month;
}
$i = $month;
}
return $dates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment