-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
$numMesAtual = $i - 1;
when1
, will get zero in+$numMesAtual month")
I didn’t quite understand your problem.– rray
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?
– Woss
All right, let’s go! I’m sorry, I’m going to try to clarify the exit I’ve been waiting for.
– Lucas Santos
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{}
– Lucas Santos
Yes, why did you make this condition if you don’t want it to happen?
– Woss
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.
– Lucas Santos