Loop autoincrementing date

2

How can I make a auto increment of date by month and showing the last day of the month

Example:

  

2017-01-31

     

2017-02-28

     

2017-03-31

$parcelas  = 12;
$data_inicial = 2017-01-31
for ($i = 1; $i <= $parcelas; $i++){        
    // abaixo o auto increment
    echo $data_inicial.........??????
}
    
asked by anonymous 20.12.2016 / 17:12

4 answers

4

To always catch the last date of the month use date with mktime() :

  

date('t', mktime());

Generate installments by the last day of the month according to the year's information:

function gerar_parcelas($y = 2017) 
{
    $parc = array();        
    for($i = 1; $i < 13;$i++){
        $parc[] = date('Y-m-t', mktime(0,0,0,$i,1,(int)$y));
    }
    return $parc;
}

var_dump(gerar_parcelas(2017));

Example

In this other code it is generated as if it were payment parcels , that is, if it was informed 12/13/2005 and the number of parcels the algorithm generates from this date only changing the month and year if it goes to the next. This code also fits your question if you enter the last date of the month with 12 plots.

Generate installments for a date:

<?php

function parcelas($data, $numero = 12)
{
    $parc = array();
    $parc[] = $data;
    list($ano, $mes, $dia) = 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-%02d", $ano, $mes, $tira);
    }
    return $parc;
}


$data = "2017-01-31";

var_dump(parcelas($data, 12));

Example

In this second example is generated by the date so to work equal is in the question always have to go through the last day of the first month of the year because, this code generates for any day of the month different from the first that takes the year and generates the last date of the month of that year.

References:

20.12.2016 / 20:13
1

You can do so

<?php
$parcelas = 12;
$data_inicial = '2017-01-31';
for ($i = 1; $i <= $parcelas; $i++){        
    echo $data_inicial."<br>";
    $d = new DateTime( $data_inicial );
    $d->modify( 'last day of next month' );
    $data_inicial = $d->format( 'Y-m-d' );
}
    
20.12.2016 / 17:58
0

Kill a fly with a cannonball ?! Well, I thought of a very simple way to solve your problem that you would use strtotime() . See the code below:

<?php    
function returnDates($parcelas, $ultima_data){
    for ($i = 1; $i <= $parcelas; $i++){   
       $date = strtotime("+$i month", strtotime($ultima_data));
       echo date("Y-m-t", $date)."\n";
    } 
}

echo returnDates(20,"2007-02-12");

In the above example I call the function by placing the number of parcels and the start date.

See example running on ideone .

    
20.12.2016 / 20:49
-1
         $m=0; 
         for($i=1;$i <= $nparcelas; $i++){
            $a1 = new DateTime($data_inicial);
            $a2 = new DateInterval('P'.$m.'M');
            $a1->add($a2);
            $a1->format("Y-m-d") -> formatação datetime para inserir no banco
            $m++;
         }
    
20.12.2016 / 18:43