Skip to content

Date modify returns invalid datetime #11368

Closed

Description

@adamwpe

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

Activity

  1. hormus commented on Jun 6, 2023

    @hormus

    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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions