Default due date - PHP

2

I researched this question here on the portal and, although it contains similar questions, I did not find anything in the sense I need. I also did not find anything that would help my problem exactly.

Based on the date of the first installment, I need to automatically calculate the expiration date of n installments, with these being the fixed day for all of them.

Example:

First installment: 9/25/2016

By increasing 30 days to this date, the due date for each month may vary, depending on the size of the month. In addition, it can happen the case of some month being "skipped", as in the month of February.

Then I need to do something in the sense below:

First installment: 1/30/2016

The second installment should be: 29/2/2016 The third installment should be: 3/30/2016 The fourth installment should be: 04/30/2016 And so on.

What do you guys tell me? Can you help me with this? Any suggestions are welcome.

Abcs

    
asked by anonymous 19.09.2016 / 02:04

1 answer

3

It would be logical to always check the generated date is valid:

<?php

function parcelas($data, $numero)
{
    $parc = array();
    $parc[] = $data;
    list($dia, $mes, $ano) = explode("/", $data);
    for($i = 1; $i < $numero;$i++)
    {
        $mes++;
        if ((int)$mes == 13)
        {
            $ano++;
            $mes = 1;
        }
        $tira = $dia;
        while (!checkdate($mes, $tira, $ano))
        {
            $tira--;
        }
        $parc[] = sprintf("%02d/%02d/%s", $tira, $mes, $ano);
    }
    return $parc;
}


$data = "29/01/2015";

var_dump(parcelas($data, 13));

Example

    
19.09.2016 / 02:45