php rsort分钟之间的时差

时间:2015-07-15 00:32:09

标签: php date datetime

我正在尝试对时间进行排序以获得中位数,它运行正常 但是...分钟,如果我有01小时2分钟它排在01小时15分钟以上

do {

$date1 = new DateTime($row_vdata['Actual Attend Date']);      
$date2 = new DateTime($row_vdata['Actual Perm Repair Date'])   
$diff = date_diff($date1,$date2);
$responsetimes[$sum]=$diff->format('%H Hour %i Minute');
$sum = $sum + 1;

}while( $row_vdata = $vdata->fetch());

rsort($responsetimes);

foreach ($responsetimes as $key => $val) {
    echo "$key = $val\n";
}

非常感谢您的帮助

结果样本

0 = 23 Hour 30 Minute
1 = 22 Hour 55 Minute
2 = 22 Hour 0 Minute
3 = 21 Hour 6 Minute
4 = 21 Hour 40 Minute
5 = 20 Hour 45 Minute
...

我想要什么

0 = 23 Hour 30 Minute
1 = 22 Hour 55 Minute
2 = 22 Hour 0 Minute
3 = 21 Hour 40 Minute
4 = 21 Hour 6 Minute
5 = 20 Hour 45 Minute
...

1 个答案:

答案 0 :(得分:1)

不是将格式化的日期保存到阵列中,而是更好地保存小时和分钟,因此更容易对阵列进行排序,如果要打印阵列,可以根据需要格式化日期,例如

do {

    $date1 = new DateTime($row_vdata['Actual Attend Date']);      
    $date2 = new DateTime($row_vdata['Actual Perm Repair Date'])   
    $diff = date_diff($date1,$date2);
    $responsetimes[$sum] = $diff->format("%H:%i");
    $sum++;

} while($row_vdata = $vdata->fetch());


usort($responsetimes, function($a, $b){
    if(strtotime($a) == strtotime($b))
        return 0;
    return strtotime($a) < strtotime($b) ? 1 : -1;
});

foreach ($responsetimes as $key => $val) {
    echo $x++ . " ";
    echo $key . " = " . date("H \H\o\u\\r i \M\i\\n\u\\t\\e", strtotime($val)) . "\n";
}