How to increase 30 days to a date by saving data in a table? [duplicate]

0

Greetings, my problem is that when I insert the dates to pay for each person I must first insert the start date of the rent, then to that same date increase one month and so with the other dates. For example, the user rents the premises for 4 months, the first payment date will be 05-09-2017, then the next date will be 05-10-2017 and will increase according to the months that the rent is made. I've tried this but it does not make me the increases:

$estado=0;
$aumento = 0.2;
$aumentaCada = 3;
$concepto = 0;

for($i = $aumentaCada; $i < $repetir; $i+$aumentaCada){
  if(($i>0) && ($i % $aumentaCada)==0):
    $monto = $monto*(1+$aumento);
  endif;

  $primer = date('d-m-Y', strtotime("$primer + 30 day"));
  $concepto = $concepto +1;

  $query3 = "INSERT INTO 
  pagos_usuarios(id_inscripcion,concepto,monto,estado,fecha_a_pagar)
  VALUES('$val2','$concepto','$monto','$estado','$primer')";
  $sql3=$mysqli->query($query3);
  }

That's how it is, but that way I do not insert anything, and it stays like an infinite bug.

    
asked by Alejo Mendoza 04.09.2017 в 00:11
source

2 answers

1

Consider these adjustments, $i will be every month

$estado=0;
$aumento = 0.2;
$aumentaCada = 3;
$concepto = 0;

//fecha inicial
$primer=date('d-m-Y');

//for inicia en mes 1, se incrementa de 1 en 1 porque debes registrar por cada mes
for($i = 1; $i < ($repetir+1); $i++){
    //monto incrementa solo en multiplos del 3er mes
    if(($i % $aumentaCada)==0){
        $monto = $monto*(1+$aumento);
    }
    //el primer insert debe tener la primera fecha, entonces se suma a partir del 2do mes
    if($i>1){
        $primer = date('d-m-Y', strtotime("$primer + 30 day"));
    }

    $concepto = $concepto +1;

    $query3 = "INSERT INTO 
    pagos_usuarios(id_inscripcion,concepto,monto,estado,fecha_a_pagar)
    VALUES('$val2','$concepto','$monto','$estado','$primer')";
    $sql3=$mysqli->query($query3);
}
    
answered by 04.09.2017 / 02:47
source
0

Changing the logic of your function a bit can be solved.

for($i = 0; $i < $repetir; $i++)
if(($i>0) && ($i % $aumentaCada)==0):

Look at the problems that the for has with the if:
If i is greater than 0 (then why not put the case 0 outside)
If it is a multiple of each how much it increases, then why not increase i by how much does it really increase?
the for should be $i = aumentacada; $i < $repetir; $i+aumentacada

With that you save the if inside the for and also the problems that you are having later.

    
answered by 04.09.2017 в 01:28