Loop autoincrementing date

Asked

Viewed 978 times

2

How can I make a auto increment date per 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.........??????
}
  • 2

    http://answall.com/a/38633/91

  • Ok interesting o’t'.... more i how to do it in sequence

  • 1

    Try something like that inside your for: echo date('Y-m-t', strtotime("2017-" . ($i < 10 ? "0$i" : $i) . "-01")) . "\n";

4 answers

4

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

date('t', mktime());

Generate plots by the last day of the month according to the information of the year:

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 is generated as if it were instalments of payment, that is to say, whether 13/12/2005 and the amount of plots the algorithm generate from that date only changing the month and year if it goes to the next. This code also fits your doubt if you are informed the last date of the month with 12 plots.

Generate plots by 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 the same is in the question always has to pass the last day of the first month of the year, because, this code generates for any day of the month other than the first that takes the year and generates the last date of the month of that year.

References:

1

You can do it like this

<?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' );
}

0

Kill a fly with a cannonball?! Well, I thought of a very simple way to solve your problem that would be you 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 installments and the initial date.

See the example functioning on the ideone.

  • @Fabio-Enrique Then try this way here and tell me what you think! Abs.

-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++;
         }
  • you tested the code ? in the test I did the variable $m does not exist, changing it to $i has a problem of month 1 to 2 for having only 28(or 29) days.

  • creates it before $m=0; and at the end of $m++; so the loop starts with the current month and has auto increment from 1 to 1

  • I get it, but the problem of losing the dates continues, run your code to see the result.

  • i use it.. it will auto increment as long as you want $m be increment be ++ (from 1 in 1) or $m = $m +2 ; and per ae will...

  • When you have time do the test simulating that is on the 29th, 30th or 31st. In other days works normally.

  • Strange here worked normal, generated 3 installments from the day 29/11 and he generated the 3 consecutive normal. I don’t see what could be doing this loss of sequence

  • Makes with 4 installments, there will fall in the month of February that has 28 days, and it will jump to the next month.

  • a yes, but there need to treat these exceptions within the for, for every rule there is an exception, besides that if you have this need for precision you may be interested in treating holidays and so on....

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.