I have a problem like this. For a given date I need to get the last day of each of the previous months up to 5 months. As an example, if the input date is 2018-08-21, then the result I want is something like (2018-07-31,2018-06-30,2018-05-31,2018-04-30,2018-03-31)
I wrote a for loop to iterate for 5 times and used the following code to get the previous month. But at months of 31 days, it doesn't exactly give the previous month. It gives the last day as "2011-07-31" which is not true. Is there a workaround for this one??
$datetocheck = "2011-07-31";
$lastday = date('Y-m-t', strtotime('-1 month', strtotime($datetocheck)));
echo $lastday; //this gives 2011-07-31(Expected value is 2011-06-30)