如何在一毫秒内获得两个日期之间的范围:
我有一个正确答案的例子:
time_start = 2018-12-18 12:33:53.1231;
time_end = 2018-12-18 12:34:10.1523;
result = 17.0292;
我要问的是,计算它的公式是什么?
我尝试了一些代码,但结果是16.708
$dt_start = 2018-12-18 12:33:53.1231;
$dt_end = 2018-12-18 12:34:10.1523;
$ex_start = explode(".",$dt_start);
$ex_end = explode(".",$dt_end);
$datetime_start = strtotime($ex_start[0]);
$datetime_end = strtotime($ex_end[0]);
$total_detik = $datetime_end-$datetime_start;
// dd($ex_end[0]);
$milis_start = floatval($ex_start[1]);
$milis_end = floatval($ex_end[1]);
$range_milis = $milis_start-$milis_end;
$new_range_milis = $range_milis/10000;
// dd($range_milis);
$second = $datetime_end-$datetime_start+$range_milis/10000;
dd($second);
//输出'16 .708'
答案 0 :(得分:1)
尝试此操作以查找两次之间的时差。
<?php
function calculateTransactionDuration($startDate, $endDate)
{
$startDateFormat = new DateTime($startDate);
$EndDateFormat = new DateTime($endDate);
$uDiff = ($startDateFormat->format('u') - $EndDateFormat->format('u')) / (1000 * 1000);;
$diff = $startDateFormat->diff($EndDateFormat);
$s = (int) $diff->format('%s') - $uDiff;
$i = (int) ($diff->format('%i')) * 60;
$h = (int) ($diff->format('%h')) * 60 * 60;
return sprintf('%.6f', abs($h + $i + $s));
}
$time_start = '2018-12-18 12:33:53.1231';
$time_end = '2018-12-18 12:34:10.1523';
echo $difference = calculateTransactionDuration($time_start, $time_end);
// output = 17.029200