【问题标题】:How to add times dynamically in php如何在php中动态添加时间
【发布时间】:2015-01-31 09:00:29
【问题描述】:
我正在获取像 9:55 或 00: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实现这个家伙。
任何身体想法?
【问题讨论】:
标签:
php
html
datetime
foreach
【解决方案1】:
首先以秒为单位将所有持续时间相加,然后使用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 );
【解决方案2】:
根据您的逻辑,您尝试添加 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);