2

If I have an object of type DateTime how I can add some milliseconds?

$date = new Datetime("2016-09-23T20:48:16.090Z");
// how to add to this date 9 milliseconds?
Cœur
  • 34,719
  • 24
  • 185
  • 251
nicolae-stelian
  • 314
  • 2
  • 12

1 Answers1

2

As of PHP 7.1, milliseconds and microseconds can also be easily added to DateTime using the modify method.

$date = new Datetime("2016-09-23T20:48:16.090Z");
$millisec = 9;

$date->modify('+ '.$millisec.' milliseconds');
var_dump($date);

Output:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2016-09-23 20:48:16.099000"
  ["timezone_type"]=>
  int(2)
  ["timezone"]=>
  string(1) "Z"
}
jspit
  • 5,731
  • 1
  • 7
  • 15