datetime - PHP: Calculating Monthly/Yearly Billing Dates - Same day next month -
i've been looking way accurately calculate next/future billing date in way month not skipped. example jun 31 want next billing date feb 28 (or 29 when applicable). found great solution. here update of code support more 1 month in future. hope helps.
code:
function calculatebillingdate($startdate, $numberofcycles = 1) { $currentmonth = date('n', $startdate); $nextmonth = (($currentmonth + $numberofcycles) % 12); if ($nextmonth === 0) { $nextmonth = 12; } $targetdate = new \datetime(); $targetdate->settimestamp($startdate); $targetdate->add(new \dateinterval('p' . $numberofcycles . 'm')); while ((int)$targetdate->format('m') !== $nextmonth) { $targetdate->sub(new \dateinterval('p1d')); } return $targetdate; }
Comments
Post a Comment