php - How to get same day of the next months? -
for recurring payment (monthly) need same date of next months, o.n., 1st of every month or 15th of every month (monthly)
but it's should maintain 31,30,28 , 29th dates.
e.g., 1/31/2014(first recurring payment date) , 2/28/2014 or 2/29/2014(2nd date), 3/31/2014 (3rd ) ... on )
e.g .1/15/2014 , 2/15/2014 , 3/15/2014 , 4/15/2014 , 5/15/2014 .. on
edited answer:
you can use dateperiod class:
this works partially:
there issue when d >= 29
, months after february have payment registered last day.
<?php error_reporting(e_all); $begin = new datetime('2014-01-29'); $lastdayinterval = dateinterval::createfromdatestring('last day of next month'); $monthinterval = new dateinterval('p1m'); $lastdays = new dateperiod(clone $begin, $lastdayinterval, 12, dateperiod::exclude_start_date); $addedmonthdays = new dateperiod(clone $begin, $monthinterval, 12, dateperiod::exclude_start_date); $lastdaysarray = array(); foreach ($lastdays $lastday) { $lastdaysarray[] = $lastday; } $addedmonthdaysarray = array(); foreach ($addedmonthdays $addedmonthday) { $addedmonthdaysarray[] = $addedmonthday; } ($i = 0; $i < 12; $i++) { if ($addedmonthdaysarray[$i] > $lastdaysarray[$i]) { echo $lastdaysarray[$i]->format('y-m-d') . php_eol; } else { echo $addedmonthdaysarray[$i]->format('y-m-d') . php_eol; } }
outputs:
2014-02-28 2014-03-31 2014-04-30 2014-05-31 2014-06-30 2014-07-31 2014-08-31 2014-09-30 2014-10-31 2014-11-30 2014-12-31 2015-01-31
with:
$begin = new datetime('2014-01-28');
it outputs:
2014-02-28 2014-03-28 2014-04-28 2014-05-28 2014-06-28 2014-07-28 2014-08-28 2014-09-28 2014-10-28 2014-11-28 2014-12-28 2015-01-28
Comments
Post a Comment