例如:
$date1='2017-12-13 13:06:21.602';
$date2='2017-12-13 13:06:18.102';
$diff=$date1-$date2
echo $diff;
输出应为3.5
答案 0 :(得分:1)
注意:此解决方案自PHP 7.1.0起添加参数f
。
// your code goes here
$date1 = new DateTime('2017-12-13 13:06:21.602');
$date2 = new DateTime('2017-12-13 13:06:18.102');
$interval = $date1->diff($date2);
echo $interval->s+$interval->f;
// s = seconds, f = microseconds, as a fraction of a second
//var_dump($interval);
结果是3.5
自4.3.0以来的PHP版本比较:https://3v4l.org/VCI6P
答案 1 :(得分:0)
这是一个解决方案5.2.2< php版本< 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);