0

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 :).

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149

4 Answers4

2

You could just use the built in function for php

date('t', $timestamp);

So your function would look like:

function last_day_of_month($month = '', $year = '') {
    $month
        || $month = date('m');
    $year
        || $year = date('y');
    $timestamp = mktime(0, 0, 0, $month, 1, $year);
    return date('Y-m-t', $timestamp);
}
Tom
  • 3,031
  • 1
  • 25
  • 33
1

To get the last day of the month, use:

date('Y-m-t', strtotime("$year-$month-01"))

't' stands for the "number of days in the given month", i.e. for the last day. No need to add and subtract anything.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

The below will output a select box with options for the last day of each month in the current year:

echo "<select>";    
for($i=1;$i<=12;$i++){    
     echo "<option>".date('Y-m-t', strtotime(date("Y")."-".$i."-01"))."</option>";    
}    
echo "</select>";

If you want to set to a different year, simply replace date("Y") with, say, "2010". You could, for example loop through a number of years, running the code to output the options for each.

SW4
  • 69,876
  • 20
  • 132
  • 137
0

I think this is the problem of your piece of code:

the original $increm_date is "2011-06-01";

a a certain point in the while cycle $increm_date is 2011-08-31 (last day of august)

the in the cycle you add a month so $increm_date is 2011-10-01 (first day of october, NOT THE LAST OF SEPTMEBER) so you call the last_day_of_month providing OCTOBER NOT SEPTEMBER.

I am not sure of this. To test try to print the $increm_date before last_day_of_month is called

ab_dev86
  • 1,952
  • 16
  • 21