php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Date modify returns invalid datetime

Open adamwpe opened this issue 3 years ago • 1 comments

Description

Example: https://stackoverflow.com/questions/76384161/php-date-modify-returned-negative-microseconds

The following code:

<?php

$datetime = date_create('now');
$datetime->setTime(1,1,1,1 /* If set to any other number, it works fine */);
$datetime->modify('-100 ms');

var_dump($datetime);

Resulted in this output:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2023-06-04 01:01:01.-99999"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

But I expected this output instead:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2023-06-04 01:01:00.900001"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

PHP Version

7.2 and greater

Operating System

No response

adamwpe avatar Jun 04 '23 12:06 adamwpe

https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L181 1000 ms is 1000000 microsecond and 1 ms is 1000 microsecond php 7.1 added microsecond and plural :)

<?php

$datetime = date_create('now');
$datetime->setTime(1,1,1,1 /* If set to any other number, it works fine */);
var_dump($datetime);
$datetime->modify('-100 ms');

var_dump($datetime);

Expected result:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2023-06-06 01:01:01.000001"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2023-06-06 01:01:00.900001"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

hormus avatar Jun 06 '23 20:06 hormus