Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96770 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36288 invoked from network); 8 Nov 2016 12:44:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Nov 2016 12:44:17 -0000 X-Host-Fingerprint: 77.165.170.254 ip4da5aafe.direct-adsl.nl Received: from [77.165.170.254] ([77.165.170.254:9074] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 19/91-23587-E98C1285 for ; Tue, 08 Nov 2016 07:44:16 -0500 Message-ID: <19.91.23587.E98C1285@pb1.pair.com> To: internals@lists.php.net References: <46.92.05967.9AB91285@pb1.pair.com> Date: Tue, 8 Nov 2016 13:44:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 77.165.170.254 Subject: Re: [PHP-DEV] DateTime microseconds discussion From: arjen@parse.nl (Arjen Schol) Hi Nikita, The https://3v4l.org/YUhFF example was to demonstrate it is NOT easy to set microseconds to zero. NOT to demonstrate new DateTime("5 minutes ago") == new DateTime("5 minutes ago"). new DateTime("5 minutes ago, 0 microseconds") indeed sets the microseconds to zero (nice to know) but this does not work for 'now'. See https://3v4l.org/8VE7W for new DateTime('now, 0 microseconds'); And this code fails < 7.1RC4. Arjen On 11/08/2016 01:24 PM, Nikita Nefedov wrote: > Hey Arjen, > > On 8 November 2016 at 09:32, Arjen Schol wrote: >> There is no easy way to set microseconds to 0, you have to call setTime > > There 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")); > ```