我正在尝试排序时间以获得中位数,它工作得很好但是…分钟,如果我有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 = $valn";
}
非常感谢您的帮助
结果样本
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
...
与其将格式化的日期保存到数组中,不如只保存小时和分钟,这样更容易对数组进行排序,如果你想打印数组,你可以按照自己的需要对日期进行格式化,例如
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 Hou\r i Mi\nu\t\e", strtotime($val)) . "n";
}