4
I do not know if it is a bug, I searched, I did not find it. But today, I need to recover a data from a month ago and to my surprise it happened this:
$dateTime = new DateTime();
print_r($dateTime);
DateTime Object
(
[date] => 2020-03-30 20:56:09.760949
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
$dateTime->modify('-1 month');
print_r($dateTime);
DateTime Object
(
[date] => 2020-03-01 20:56:09.760949
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
and if recovering a previous day works normally:
$dateTime = new DateTime('2020-03-29 20:00:00');
print_r($dateTime);
DateTime Object
(
[date] => 2020-03-29 20:00:00.000000
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
$dateTime->modify('-1 month');
print_r($dateTime);
DateTime Object
(
[date] => 2020-02-29 20:00:00.000000
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
and realized March 30 and 31 did not return February 29. Someone has some solution to this problem?
It’s not a bug, take a look at the example in documentation
– Rafael Tavares
thanks. thanks
– Paulo Rogerio
In practice it is a "bug admitted by language" :P - In these cases it pays to take the previous month (based on day 1, for example), and then extract the last day of the current month, or some other maneuver that is deterministic. PHP is bad for dealing with dates. It has a lot of function, but it’s absurd how they are implemented internally.
– Bacco