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?
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?
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"
}
For lower PHP versions like PHP 5.6 you could try:
date_default_timezone_set('UTC');
$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d\TH:i:s",$date_array[1]);
echo $date."." .round( $date_array[0] * 1000)."Z";