Date +1 Month increment (considering days, month and year)

Asked

Viewed 294 times

-1

Here’s the thing, in a system I’m developing, I need to increment a date. Below follows the code that I managed to develop until the moment that this works perfectly for increment even considering the days "29 - 31", but when I put day "05" it Buga as the year passes, because it does not validate the first if.

for($i = 1; $i <= 12; $i++){ 
        $data = '2019-01-05'; // TESTE 02 '2019-01-31'
        if($i >= 1){ 
            $numMesAtual = $i - 1;
            $mesAtual = date("m", strtotime(date("Y-m-d", strtotime($data)) . "+$numMesAtual month"));
            $mesProximo  = date("m", strtotime(date("Y-m-d", strtotime($data)) . "+$i month"));

            if($mesAtual == $mesProximo-1){
                $data = date("Y-m-d", strtotime(date("Y-m-d", strtotime($data)) . "+$i month"));
            } else{
                $data = date('Y-m-d', strtotime(date("Y-m-d", strtotime($data)) . "last day of +$i month"));
            }
        }
        echo $i ."- ". $data ."<br/>";
    }
---------------------
RETORNO ESPERADO
---------------------
1- 2019-02-05
2- 2019-03-05
3- 2019-04-05
4- 2019-05-05
5- 2019-06-05
6- 2019-07-05
7- 2019-08-05
8- 2019-09-05
9- 2019-10-05
10- 2019-11-05
11- 2019-12-05
12- 2020-01-05 - //Porém o código atual retorna 2020-01-31, porque entra no else{}, isso ocorre porque ao passar de ano o número do mês e tornou 1 
  • 2

    $numMesAtual = $i - 1; when 1, will get zero in +$numMesAtual month") I didn’t quite understand your problem.

  • 1

    Also I could not understand the problem. I ran here and the output seemed to be consistent with the code. What was the expected output?

  • All right, let’s go! I’m sorry, I’m going to try to clarify the exit I’ve been waiting for.

  • 1-2019-02-05 2-2019-03-05 3-2019-04-05 4-2019-05-05 5-2019-06-05 6-2019-07-05 7-2019-08-05 8-2019-09-05 9-2019-10-05 10-2019-11-05 11-2019-12-05 12-2020-01-31 - Here’s the problem, Because the year is different, he enters Isis{}

  • Yes, why did you make this condition if you don’t want it to happen?

  • I made to validate when the day is between 29-31 and does not exist in any month, for example in January we have 31 days, while in February 28, in this case it would increment to the last day of next month.

Show 1 more comment

1 answer

0


This occurs at every turn of the year, the reason is its comparison:

if($mesAtual == $mesProximo-1){

The $mesAtual will be 11 and the next month would be 0. A correction would be to compare the year, as:

$anoAtual = date("Y", strtotime(date("Y-m-d", strtotime($data)) . "+$numMesAtual month"));
$anoProximo  = date("Y", strtotime(date("Y-m-d", strtotime($data)) . "+$i month"));

if($mesAtual == $mesProximo-1 || $anoAtual != $anoProximo) {

Although, I believe the code still doesn’t make much sense.

  • So, that’s the way I found to do, I’m making a system that has installments, when the person parcel in "X Months" I need to increment the dates, but if they are days of end of month, I need that function, because often there is no day 31, in this case I want the installment to be on the last day next month, if find another better solution, I am open to suggestions, but anyway thank you very much, your code solved my problem.

  • @Lucassantos, personally I never went through this (since I always made collections every 30 days, not "every month"). I think a better solution would be to set another starting date when the starting day is > 28. That is, installments can only be set between day 1 until 28, so the user also already knows when to pay. If he start the contract on the day 31/07, all the next invoices would refer to the day 28 (28/08, 28/09, 28/10...), hence would have no problem. That way, just check whether the day of $data is > 28, if it is set to 28 and then only sum with 1 month.

Browser other questions tagged

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