Re: DateTime microseconds discussion
| From: | Nikita Nefedov | Date: | Tue, 08 Nov 2016 12:24:02 +0000 |
| Subject: | Re: DateTime microseconds discussion | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96768@lists.php.net to get a copy of this message | ||
Hey Arjen,
On 8 November 2016 at 09:32, Arjen Schol <arjen@parse.nl> wrote:
There is no easy way to set microseconds to 0, you have to call setTimeThere actually is an easy way, you can pass microseconds absolute value in the constructor as well, it would like: `new DateTime("5 minutes ago, 0 microseconds")` As Dan said it is probably a mistkae in the code, really, and it is a very popular one, to think that `new DateTime("5 minutes ago") == new DateTime("5 minutes ago")`. If you sample it enough times even on pre-7 versions of PHP you will get inequality as well: https://3v4l.org/JV59e (sorry for overloading 3v4l a bit here). It is especially an undesirable mistake because it causes failures randomly, no one likes their test suite failing /sometimes/, it makes debugging a very unpleasant experience. When doing date manipulations, let's say the task is to generate a report for the previous month, you need to always have a base date as a point of reference:
$startDate = new DateTimeImmutable("first day of previous month, 00:00:00.0");
$endDate = $startDate->add(new DateInterval("P1M"));