How to catch the 29th of February php datetime

Asked

Viewed 56 times

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

  • thanks. thanks

  • 1

    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.

1 answer

3


I researched more on the subject and the solution I found was:

$hoje =  new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
$mesPassado = $hoje->modify('last day of previous month');

print_r($mesPassado->format('Y-m-d'));

[solved]

  • I abhor regular use of PHP Datetime, but in the specific scenario it is worth the +1 by deterministic solution.

  • But why do you abhor the use of Datetime in php ? Use is widely encouraged.

  • 1

    Encouraged by practicality and belief (even belief, it seems that almost only based on faith) that OOP is better than without OOP, not saying that procedural is better, nor does this comparative if it is to speak of general use, I’m saying that OO has time and place to use, but the question I suppose is Bacco’s comment is to say how people use it indiscriminately when they could use the simple that also works, I mean it has useful resources within the class, but most end up using it and end up q gets a use for exaggeration, it is not pq most encourages q they know what they do.

  • I’m talking about the incentive. But because of the OOP and nothing else

  • Datetime has an immense and unnecessary overhead for most cases, and when you combine that with string interpretation, the problem gets much worse. In this case here, for lack of better functions, it is acceptable as a solution (efficiency is detail). And Datetime has nothing like "OOP", although it uses object syntax. @Guilhermebirth in PHP can safely say that procedural is always better (it’s the only thing that makes sense). OOP is directly related to state maintenance, PHP is a basically scripting language. ie without state maintenance. That sums up a lot.

  • And I explained, encouragement for no reason, because most don’t even know what they’re doing, they only see a new way of doing something and even assume that other media are obsolete, even if they’re not, just by assuming, the PHP community is full of myths (invented stories of the head and assume things), I remember they said that require is slower than include, that include is quicker than include_once, the staff would change the codes for things like this... of the same people who "encourage", but that’s not all, i can even one day enumerate the amount of incentives without foundations of the "php community"

  • Anyway, the place here is not for chat, if you are interested in participating in the chat: https://chat.stackexchange.com/rooms/11910/pop-of-pilha -- there you can take your doubts on the subject, welcome

Show 2 more comments

Browser other questions tagged

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