php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78673 Wrong results for DateTime->modify() with milliseconds
Submitted: 2019-10-14 13:55 UTC Modified: 2019-10-15 19:31 UTC
From: dse at newpharma dot ro Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2019-10-14 13:55 UTC] dse at newpharma dot ro
Description:
------------
Adding or subtracting milliseconds from a DateTime object does not give the correct result.

Test script:
---------------
<?php

$millisInThePast = 50000;

$currentDate = new DateTime("2019-10-14T15:08:23.123+02:00");
echo 'Current: ' . $currentDate->format(DATE_RFC3339_EXTENDED) . "\n";
echo 'Past:    ' . $currentDate->modify("-{$millisInThePast} msec")->format(DATE_RFC3339_EXTENDED);

Expected result:
----------------
Current: 2019-10-14T15:08:23.123+02:00
Past:    2019-10-14T15:07:33.123+02:00

Actual result:
--------------
Current: 2019-10-14T15:08:23.123+02:00
Past:    2019-10-14T15:08:21.-47877+02:00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-14 14:44 UTC] sjon@php.net
-Status: Open +Status: Verified
 [2019-10-14 14:45 UTC] cmb@php.net
See also <https://3v4l.org/FVLs5>.

Looks like the result is not normalized.
 [2019-10-15 09:13 UTC] dse at newpharma dot ro
Just want to add that same issue also happens with microseconds:
https://3v4l.org/Usapt
 [2019-10-15 10:44 UTC] derick@php.net
The code definitely normalizes the fractions:

void timelib_do_normalize(timelib_time* time)
{
    if (time->us != TIMELIB_UNSET) do_range_limit_fraction(&time->us, &time->s);

So I'm going to have to see why this doesn't work.
 [2019-10-15 19:31 UTC] derick@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: derick
 [2019-10-15 19:31 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

This has been fixed in timelib now, which will soon make it into PHP too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC