7
In this code PHP, I am checking the type of recurrence of an activity contained in a list of activities, and adding 1 month when it is monthly recurrence, 2 months when it is bimonthly recurrence and etc.
if($recorrencia == "UNICA")
return;
elseif($recorrencia == "MENSALMENTE")
$data_vencimento = date('Y-m-d H:m:s', strtotime('+1 month', strtotime($atividade['DT_VENCIMENTO']))); /*1 mês*/
elseif($recorrencia == "BIMESTRALMENTE")
$data_vencimento = date('Y-m-d H:m:s', strtotime('+2 month', strtotime($atividade['DT_VENCIMENTO']))); /*2 mês*/
elseif($recorrencia == "TRIMESTRALMENTE")
$data_vencimento = date('Y-m-d H:m:s', strtotime('+3 month', strtotime($atividade['DT_VENCIMENTO']))); /*3 mês*/
elseif($recorrencia == "SEMESTRALMENTE")
$data_vencimento = date('Y-m-d H:m:s', strtotime('+6 month', strtotime($atividade['DT_VENCIMENTO']))); /*6 mês*/
elseif($recorrencia == "ANUALMENTE")
$data_vencimento = date('Y-m-d H:m:s', strtotime('+12 month', strtotime($atividade['DT_VENCIMENTO']))); /*12 mês*/
However, the PHP looks like it’s actually adding 30 days, because when I have dates like the following:
31/05/2019
After rotating the function, the date becomes:
01/07/2019
I understand that is why in the month 06 does not have day 31, because all other days add correctly.
How could I get around this problem?
No time to write an answer now, but I am sorry to inform you that you will probably have to make a manual adjustment: https://stackoverflow.com/a/5760371
– hkotsubo
You can’t use days instead of months?
– Edward Ramos
Check here https://stackoverflow.com/questions/38226215/how-to-include-the-end-date-in-a-dateperiod . Search for Dateinterval and Dateperiod
– Marcos Xavier
If it fails in the months of 30, then also in February!?
– rbz
@hkotsubo I will try to implement here something similar to that of the link
– Denied
@Edwardramos would give in the same.
– Denied
@Rbz Yes, exact.
– Denied
@Denied Tested on which versions of PHP?
– rbz