Created
June 6, 2013 18:31
-
-
Save plasticbrain/5723781 to your computer and use it in GitHub Desktop.
PHP - Get months in date range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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