I have a date range ($startDate, $endDate). Based on the range that is given I need to loop through each month and run a specific job.
Currently I am doing this as such:
//Get startOf and endOf to cover entire month of range.
$currentDate = Carbon::parse($startDate)->startOfMonth();
$endDate = Carbon::parse($endDate)->endOfMonth();
//Number of weeks helps with the for loop
$numberOfMonths = $currentDate->diffInMonths($endDate);
for ($x = 0; $x <= $numberOfMonths; $x++)
{
//something here
$currentDate = $currentDate->addMonths(1);
}
This is done with carbon and php. The problem with the current method is that diffInMonths apparently simply divides by 30 and some how skips February as it only has 28 days.
I need some sort of loop by each month. So for example if my date range was 2018-01-15 and 2018-04-25, it should loop through four times, 1/2/3/4 and stop. Currently it is going 1/3/4 and stop.
Is there some simple work around for this, that I can loop through the months within a date range?