copy()->startOfMonth()->addMonth(); } /** * Calculates the last day of the month. * * If it is the last day of the month - we add a month on. * * @param Carbon $date The start date * @return Carbon The last day of month */ public function calculateLastDayOfMonth($date) { if ($date->isLastOfMonth()) { return $date->copy()->addMonthNoOverflow()->endOfMonth(); } return $date->copy()->endOfMonth(); } /** * Sets the day of the month, if in the past we ADD a month * * @param Carbon $date The start date * @param string|int $day_of_month The day of the month */ public function setDayOfMonth($date, $day_of_month) { $carbon_date = Carbon::parse($date); $set_date = $carbon_date->copy()->setUnitNoOverflow('day', $day_of_month, 'month'); //If the set date is less than the original date we need to add a month. //If we are overflowing dates, then we need to diff the dates and ensure it doesn't equal 0 if ($set_date->lte($date) || $set_date->diffInDays($carbon_date) == 0) { $set_date->addMonthNoOverflow(); } if ($day_of_month == '31') { $set_date->endOfMonth(); } return $set_date; } }