3
I am developing an application. And I am using the Carbon API. But conflicts are happening from one month to the next. Today is July 31st. And the code I used was the following:
$inicioMesAtual = Carbon::now()->startOfMonth();
$fimMesAtual = Carbon::now()->endOfMonth();
$inicioMesPassado = Carbon::now()->subMonth()->startOfMonth();
$fimMesPassado = Carbon::now()->subMonth()->endOfMonth();
It happened that in the $fimMesAtual
gives the same result as the $fimMesPassado
The exits were like this:
Carbon @1564628399 {#350 ▼
date: 2019-07-31 23:59:59.999999 America/Sao_Paulo (-03:00)
}
Carbon @1564628399 {#352 ▼
date: 2019-07-31 23:59:59.999999 America/Sao_Paulo (-03:00)
}
This case also occurs at the beginning of the month (The beginning of this month is the same as last month). Could someone explain to me why this is happening? Before July 31, the beginning and end of last month worked well. Why is this not working until July 31? How can I fix this?
It worked fine, thank you for your answer. I went to search the github and something came up, as a tip to adjust, using PHP instead of Carbon. At first, you would consider the Carbon subMonth() as something not recommended for use?
– Arthur Abitante
I can’t say if it is recommended or not, because I’ve never used Carbon (I’ve never seen meaning in it), but we can say it’s unnecessary in this problem. I see no justification in performing arithmetic operations between dates to get something that can be obtained in a simpler way.
– Woss