如何用毫秒加两次?

时间:2015-02-06 19:44:58

标签: php

有没有办法用毫秒这样加两次:

00:01:02.73
00:03:03.01
一个人?

结果应该是这样的:

00:04:05.74

格式很简单“hours:minutes:seconds.milliseconds

如果是,怎么样?

3 个答案:

答案 0 :(得分:1)

我写了很少的函数来帮助我做到这一点

$time1 = "00:15:45.89";
$time2 = "00:18:30.22";

$milli1 = timeToMilliseconds($time1);
$milli2 = timeToMilliseconds($time2);

$new = $milli1 + $milli2;

$time = formatMilliseconds($new);
echo $time;

function timeToMilliseconds($time){
    $time_start = substr($time, -11, -3);
    $time_end = substr($time, -3);

    $time_arr = explode(':', $time_start);
    $seconds = 0;
    foreach($time_arr as $key => $val){
        if($key == 0){
            $seconds += $val * 60 * 60;
        }elseif($key == 1){
            $seconds += $val * 60;
        }elseif($key == 2){
            $seconds += $val;
        }
    }

    $seconds = $seconds.$time_end;
    $milliseconds = $seconds * 1000;

    return $milliseconds;
}

function formatMilliseconds($milliseconds) {
    $seconds = floor($milliseconds / 1000);
    $minutes = floor($seconds / 60);
    $hours = floor($minutes / 60);
    $milliseconds = $milliseconds % 1000;
    $seconds = $seconds % 60;
    $minutes = $minutes % 60;

    $format = '%u:%02u:%02u.%03u';
    $time = sprintf($format, $hours, $minutes, $seconds, $milliseconds);
    return rtrim($time, '0');
}

答案 1 :(得分:0)

您的解决方案@Scorpioniz有问题,例如,当时间为“ 1:34:55.831 ”时,formatMilliseconds的返回值为“ 1582:10:3​​1.704

我在 timeToMilliseconds 函数中进行了一些更改,我认为这更加一致:

function timeToMilliseconds($time){
    $dateTime = new DateTime($time);

    $seconds = 0;
    $seconds += $dateTime->format('H') * 3600;
    $seconds += $dateTime->format('i') * 60;
    $seconds += $dateTime->format('s');

    $seconds = floatval($seconds . '.' . $dateTime->format('u'));

    return $seconds * 1000;
}

或者如果您的PHP版本小于5.2.2,那么您可以这样做:

function timeToMilliseconds($time){
    sscanf($time, "%d:%d:%d.%d", $hours, $minutes, $seconds, $microseconds);

    $seconds += $hours * 3600;
    $seconds += $minutes * 60;

    $seconds = floatval($seconds . '.' . $microseconds);

    return $seconds * 1000;
}

我希望有人喜欢。

(如果我说了一些奇怪的话,对不起我的英语:P)

答案 2 :(得分:-2)

我发现了这个,它的代码少于你的代码,它完成了这项工作。我添加了毫秒的函数。 我想知道php是否有内置的功能。     

/**
 * @author Masino Sinaga, http://www.openscriptsolution.com
 * @copyright October 13, 2009
 */


function sum_the_time($time1, $time2) {

    $times = array($time1, $time2);
    $seconds = 0;
    $Imiliseconds = 0.0;

    foreach ($times as $time){
        list($rest, $miliseconds) = explode('.', $time);
        $Imiliseconds += '0.'.$miliseconds;
        list($hour, $minute, $second) = explode(':', $rest);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
    }

    $seconds += floor($Imiliseconds);
    $hours = floor($seconds/3600);
    $seconds -= $hours*3600;
    $minutes  = floor($seconds/60);
    $seconds -= $minutes*60;
    $miliseconds  = $Imiliseconds-floor($Imiliseconds);
    // return "{$hours}:{$minutes}:{$seconds}:{$miliseconds}";
    return sprintf('%02d:%02d:%02d:%02d', $hours, $minutes, $seconds, $miliseconds); // Thanks to Patrick
}
echo sum_the_time($time, $time2);  // this will give you a result: 19:12:25