Created
December 8, 2009 23:14
-
-
Save tkaemming/252093 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
function get_month_range($start_date, $end_date) | |
{ | |
if (!is_int($start_timestamp)) : | |
$start_timestamp = strtotime($start_date); | |
if ($start_timestamp === false) : | |
throw new Exception("Invalid value provided for the start date!"); | |
endif; | |
endif; | |
if (!is_int($end_timestamp)) : | |
$end_timestamp = strtotime($end_date); | |
if ($end_timestamp === false) : | |
throw new Exception("Invalid value provided for the end date!"); | |
endif; | |
endif; | |
if ($start_timestamp > $end_timestamp) : | |
throw new Exception("The start time cannot be greater than the end time!"); | |
endif; | |
$start_month = (int)date('n', $start_timestamp); | |
$start_year = (int)date('Y', $start_timestamp); | |
$start_month_timestamp = mktime(null, null, null, $start_month, 1, $start_year); | |
$end_month = (int)date('n', $end_timestamp); | |
$end_year = (int)date('Y', $end_timestamp); | |
$end_month_timestamp = mktime(null, null, null, $end_month, 1, $end_year); | |
$months = array(); | |
$current_month_timestamp = $start_month_timestamp; | |
while ($current_month_timestamp <= $end_month_timestamp) : | |
// This is the value that gets added to the returned array. | |
// It can be whatever you want -- string value, another array, etc. | |
// At this point in the function execution, $current_month_timestamp | |
// is a timestamp for the first day of the current month in the loop, | |
// and that's what you'll want to use to generate the return value. | |
$months[] = date('F Y', $current_month_timestamp); | |
$current_month = (int)date('n', $current_month_timestamp); | |
$current_year = (int)date('Y', $current_month_timestamp); | |
if ($current_month++ > 12) : | |
$current_month = 1; | |
$current_year++; | |
endif; | |
$current_month_timestamp = mktime(null, null, null, $current_month, 1, $current_year); | |
endwhile; | |
return $months; | |
} | |
$start_date = 'October 30, 1986'; | |
$end_date = 'June 1, 1987'; | |
$months = get_month_range($start_date, $end_date); | |
var_dump($months); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment