簡體   English   中英

使用 php 的小時和分鍾總和之間的差異

[英]Difference between sum of hours and minutes using php

我想計算總小時數和分鍾數與另一個總小時數和分鍾數的差異,例如:

$firsttotal = 181:33; ( 181 hours and 33 minutes)
$secondtotal = 315:00; (315 hours and 0 minutes)

所以我想要一種計算它的差異的方法,比如

$difference = 134:07 (134 hours and 7 minutes is the correct answer)

由於 php strtotime 沒有轉換它,而且 php 日期 function 也沒有處理這個問題。

答案是(基於兩個輸入) 133:27 ,而不是134:07

在 PHP 中,您可以通過最少的數據檢查執行以下操作。

// 181 hours and 33 minutes
$firsttotal = "181:33";
// 315 hours and 0 minutes
$secondtotal = "315:00";

// Convert to min.
// Assumes $str is a time in hhh:mm format only.
// For example, "181:33" is 181 hours and 33 min.
function toMin($str) {
    $tmp = explode(":",$str);
    return $tmp[1] + (60 * $tmp[0]);
}

// Return the difference in two min counts in HHH:MM format.
function diffToHHMM($min1,$min2) {
    $diff = $min2 - $min1;
    $h = floor($diff / 60);
    $m = ($diff % 60);
    return $h.":".$m;
}

$val1 = toMin($firsttotal); 
$val2 = toMin($secondtotal);

echo diffToHHMM($val1, $val2);
// Result: 133:27

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM