我使用Google Maps API獲取兩點之間的持續時間。該API以小時和分鐘爲單位給出持續時間。所以我希望它只用PHP代碼轉換成分鐘。有沒有解決方案?使用PHP將小時和分鐘轉換爲分鐘
例:2小時10分鐘(應該給130分鐘作爲輸出)
我使用Google Maps API獲取兩點之間的持續時間。該API以小時和分鐘爲單位給出持續時間。所以我希望它只用PHP代碼轉換成分鐘。有沒有解決方案?使用PHP將小時和分鐘轉換爲分鐘
例:2小時10分鐘(應該給130分鐘作爲輸出)
嘗試是這樣的
// Transform hours like "1:45" into the total number of minutes, "105".
function hoursToMinutes($hours)
{
$minutes = 0;
if (strpos($hours, ':') !== false)
{
// Split hours and minutes.
list($hours, $minutes) = explode(':', $hours);
}
return $hours * 60 + $minutes;
}
// Transform minutes like "105" into hours like "1:45".
function minutesToHours($minutes)
{
$hours = (int)($minutes/60);
$minutes -= $hours * 60;
return sprintf("%d:%02.0f", $hours, $minutes);
}
源:laserlight - >http://board.phpbuilder.com/showthread.php?10342598-quot-Convert-quot-hours-minutes-quot-to-quot-total_minutes-quot-and-back-quot
請稱作者, http://board.phpbuilder.com/showthread.php?10342598-quot-Convert-quot-hours-minutes-quot-to-quot-total_minutes-quot-and-back-quot – Tirolel
感謝您的幫助球員,我發現了另一種解決方案...這適用於我
$duration = "What Ever the time (2 hours 10 mins)"
$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $duration, -1, PREG_SPLIT_NO_EMPTY);
if ($split[0] >= 1 && $split[0] < 60 && $split[1] == 'min'){
$duration = $split[0];
}
elseif ($split[0] >= 1 && $split[0] < 60 && $split[1] == 'mins'){
$duration = $split[0];
}
else{
$durationHours = $split[0]*60;
$durationMin = $split[2];
$duration = $durationHours + $durationMin;
echo $duration;
}
可能的重複將hh:mm:ss轉換爲分鐘](https://stackoverflow.com/questions/24975664/how-to-convert-hhmmss-to-minutes) – Tirolel
將小時數乘以60,然後將其添加到分鐘數。 –