These answers don't quite do what I want:
Getting last month's date in php How to find the last day of the month from date?
I have the following code that is meant to print a select-box with all the last day of month dates from a given date:
$last_case_date = $this->query_end_date();
$increm_date = "2011-06-01";
echo '<select name = "end_interval">';
$increm_date = date('Y-m-d', strtotime("$increm_date"));
while($increm_date <= $last_case_date) {
// next date is $increm_date + 1 month
$increm_date = date('Y-m-d', strtotime("$increm_date + 1 months"));
// then we want the last day of this new date
$month = substr($increm_date, 5, 2);
$year = substr($increm_date, 0, 4);
$increm_date = $this->last_day_of_month($month, $year);
echo '<option value = "'.$increm_date.'" selected = "selected"'.'>'.$increm_date.'</option>';
}
echo '</select>';
where last_day_of_month
is this:
function last_day_of_month($month = '', $year = '') {
if(empty($month)) {
$month = date('m');
}
if(empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 months', $result));
return date('Y-m-d', $result);
}
which I borrowed from here.
Strange thing that happens is that I get these dates:
2011-07-31
2011-08-31
2011-10-31
but no 2011-09-30
! It probably has something to do with September only having 30 days or something right?
Can anyone spot the problem? I've been staring at it for ages.... Thank you :).