1 回答
TA贡献2003条经验 获得超2个赞
据我所知,时间戳以小时:分钟:秒的格式存储。要找到四个时间戳(游泳、自行车、跑步、总体)中的最小值,您需要首先将每个时间戳转换为秒。以下代码可用于获取四个时间戳的最佳时间:
function GetTimeInSec($time) {
list($hour, $min, $sec) = explode(":", $time);
$hour = (int) ltrim($hour, "0");
$min = (int) ltrim($min, "0");
$sec = (int) ltrim($sec, "0");
$new_time = (($hour * 3600) + ($min * 60) + ($sec));
return $new_time;
}
function GetBestTime($t1, $t2, $t3, $t4) {
$time_arr = array(
GetTimeInSec($t1) => $t1,
GetTimeInSec($t2) => $t2,
GetTimeInSec($t3) => $t3,
GetTimeInSec($t4) => $t4
);
ksort($time_arr);
$time_cols = array_keys($time_arr);
$best_time = $time_cols[0];
return $best_time;
}
$time_data = GetBestTime($row['Swim'], $row['Bike'], $row['Run'], $row['Overall']);
$best_time = $time_data["best_time"];
$avg_time = $time_data["avg_time"];
自行车、跑步和游泳的平均时间可以通过首先将每个值从 VARCHAR 转换为 INT 格式的秒数来计算。之后,array_sum 函数可用于查找游泳、自行车和跑步的总和。然后将总和除以总行数以获得平均值。可以使用以下代码:
function GetAverageTimes($data) {
$data_new = array("swim" => array(), "bike" => array(), "run" => array());
for ($count = 0; $count < count($data); $count++) {
$row = $data[$count];
array_push($data_new['swim'], GetTimeInSec($row['swim']));
array_push($data_new['bike'], GetTimeInSec($row['bike']));
array_push($data_new['run'], GetTimeInSec($row['run']));
}
$averages = array("swim" => 0, "bike" => 0, "run" => 0);
$averages['swim'] = ceil(array_sum($data_new['swim']) / count($data));
$averages['bike'] = ceil(array_sum($data_new['bike']) / count($data));
$averages['run'] = ceil(array_sum($data_new['run']) / count($data));
return $averages;
}
$average_times = GetAverageTimes($return);
- 1 回答
- 0 关注
- 109 浏览
添加回答
举报