如何在 php 中动态添加时间


How to add times dynamically in php

我正在获取视频的持续时间,例如9:5500:13:51。我想将这些值添加到循环中php

我试过像

    $t_duartion =0;
    foreach ($learn_detail->video_det as $video_det){
        $t_duartion += date("H:i:s",strtotime($video_det->duration));
    }

但结果并没有到来。它不是以hh:mm:ss格式出现的。如何在php中实现这些家伙.

任何身体想法?

先将所有持续时间(以秒为单位)相加,然后使用 date()

例:

$t_duartion = 0; // Typo in variable name btw.
foreach ( $learn_detail->video_det as $video_det ) {
    // Add duration of current video in seconds to total count.
    $t_duartion += strtotime( $video_det->duration );
}
// Convert total seconds to hh:mm:ss format.
$time = date( 'H:i:s', $t_duartion );

根据您的逻辑,您尝试添加 00:13:00 和 09:14:53 等是错误的。试试这个:

    foreach ($learn_detail->video_det as $video_det){
        $t_duartion += strtotime($video_det->duration);
    }
$t_duartion = date("H:i:s",$t_duartion);