-2

I am working on loan slider calculation in which if user select any date from drop down then i have to show the ext 6 months date. for ex: if user selects the date "5" then next 6 dates would be which are i am getting now:

1 5th Dec 2017
2 5th Jan 2018
3 5th Feb 2018
4 5th Mar 2018
5 5th Apr 2018
6 5th May 2018

But if user selects the "30" or "31" from the drop down then it skips the February month.

Please Help!!!!

prakash tank
  • 1,269
  • 1
  • 9
  • 15
  • 1
    That is special case, you need to explicitly write the code for those dates and don't forget to consider leap year case too – Rahul Oct 24 '17 at 06:08
  • 1
    https://stackoverflow.com/questions/5760262/php-adding-months-to-a-date-while-not-exceeding-the-last-day-of-the-month – Kaarel Oct 24 '17 at 06:09
  • Possible duplicate of [PHP: Adding months to a date, while not exceeding the last day of the month](https://stackoverflow.com/questions/5760262/php-adding-months-to-a-date-while-not-exceeding-the-last-day-of-the-month) – levi Oct 24 '17 at 06:11
  • Not a duplicate issue because i tried that example it's not working in my case.... – prakash tank Oct 24 '17 at 06:13
  • Uhm, but that's precisely what the duplicate is about. That none of the examples worked is left without proof in your question here. And it's not less of a duplicate even if none of the answers are complete yet. – mario Oct 24 '17 at 06:55

1 Answers1

1

Consider this function:

    function add($date_str, $months)
    {
        $date = new DateTime($date_str);


        $start_day = $date->format('j');


        for($i=1;i<=$months;$i++)
{ 
        $date->modify("+{$i} month");


        $end_day = $date->format('j');

        if ($start_day != $end_day)
        {

            $date->modify('last day of last month');
        }

        echo  $date;
    }
    }

Call add(date,6);

Blesson Christy
  • 380
  • 3
  • 13