簡體   English   中英

PHP獲取兩個數組的小時總和

[英]PHP get the sum of hours of the two arrays

我有一個像下面這樣的數組,它里面有2個具有值的數組,想知道如何獲取兩個小時的總和,例如01:00 + 04:00 = 05:00

數組:

Array
(
    [0] => stdClass Object
        (
            [total_time] => 01:00
        }
    [1] => stdClass Object
        (
            [total_time] => 04:00
        }
}

謝謝。

請按照以下步驟操作:

  • 遍歷數組;
  • 這樣HH:MM格式轉換為秒;
  • 將所有轉換的秒數相加;
  • 這樣將總秒數轉換回HH:MM格式。

有很多不同的方法可以實現這一目標。 我更喜歡使用array_map從對象中獲取值並轉換值。 對於array_sum來說,將它們加在一起應該很容易。

$hours_array = array_map("get_hours", $array);
$results = array_sum($hours_array);

function get_hours($object) {
    // Check values first

    // For just hours.
    list($hours,) = explode(":", $object->{'total_time'});

    return $hours;
}

暫無
暫無

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

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