This question already has an answer here:
For example:
$date1='2017-12-13 13:06:21.602';
$date2='2017-12-13 13:06:18.102';
$diff=$date1-$date2
echo $diff;
output should be 3.5
</div>
This question already has an answer here:
For example:
$date1='2017-12-13 13:06:21.602';
$date2='2017-12-13 13:06:18.102';
$diff=$date1-$date2
echo $diff;
output should be 3.5
</div>
收起
Here is a solution 5.2.2 < php version < 7.1
<?php
$date1=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:21.602');
$date2=\DateTime::createFromFormat('Y-m-d H:i:s.u','2017-12-13 13:06:18.102');
// $di = $date1->diff($date2);
// var_dump( $di );
// seconds
$diffSeconds = $date1->getTimestamp() - $date2->getTimestamp();
// milli
$diffMilli = ($date1->format('u') - $date2->format('u')) / 1000000;
//result :
$diffWithMilli = $diffSeconds + $diffMilli;
var_dump($diffWithMilli);
报告相同问题?