Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date modify returns invalid datetime #11368

Closed
adamwpe opened this issue Jun 4, 2023 · 1 comment
Closed

Date modify returns invalid datetime #11368

adamwpe opened this issue Jun 4, 2023 · 1 comment

Comments

@adamwpe
Copy link

adamwpe commented Jun 4, 2023

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

@hormus
Copy link

hormus commented Jun 6, 2023

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants