我正在试图弄清楚如何将一组持续时间(视频长度)加在一起以获得整个系列的最终长度。
值将采用H:i:s
格式:
$durations = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
有没有简单的方法来获得这种数组值的总时间?
答案 0 :(得分:4)
快速而肮脏的实施;
function AddTime(Array $durations)
{
$total_time = 0;
foreach($durations as $duration) {
sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds);
$total_time += isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
}
return sprintf('%02d:%02d:%02d', ($total_time/3600),($total_time/60%60), $total_time%60);;
}
$durations = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
echo AddTime($durations);
答案 1 :(得分:1)
这里我开发了持续时间计算功能。只需复制并通过它。
<?php
function duration_cal($durations)
{
$sec = 0;
foreach($durations as $du)
{
$timarr = explode(":",$du);
$hh = $timarr[0];
$mm = $timarr[1];
$ss = $timarr[2];
$sec = $sec + ($hh*60*60) + ($mm * 60) + $ss;
}
$hh = floor($sec/3600);
$mm = floor(($sec%3600) / 60);
$ss = (($sec%3600) % 60);
$hh = str_pad($hh, 2, '0', STR_PAD_LEFT);
$mm = str_pad($mm, 2, '0', STR_PAD_LEFT);
$ss = str_pad($ss, 2, '0', STR_PAD_LEFT);
return $hh.":".$mm.":".$ss;
}
$durations = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
echo duration_cal($durations);
?>
此网址中的工作脚本:http://sugunan.net/demo/duration.php
答案 2 :(得分:1)
$durations = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
$startDate = new DateTime('2000-01-01', new DateTimeZone('America/Los_Angeles'));
$endDate = new DateTime('2000-01-01', new DateTimeZone('America/Los_Angeles'));
foreach ($durations as $duration) {
list($H, $i, $s) = explode(':', $duration);
$endDate->add(new DateInterval(sprintf('PT%sH%sM%sS', $H, $i, $s)));
}
echo $startDate->diff($endDate)->format('%H:%I:%S');
吐出:00:20:28
答案 3 :(得分:0)
从来源找到并获取/借用:
http://www.daveismyname.com/adding-multiple-times-together-in-an-array-with-php-bp
<?php
class times_counter {
private $hou = 0;
private $min = 0;
private $sec = 0;
private $totaltime = '00:00:00';
public function __construct($times){
if(is_array($times)){
$length = sizeof($times);
for($x=0; $x <= $length; $x++){
$split = explode(":", @$times[$x]);
$this->hou += @$split[0];
$this->min += @$split[1];
$this->sec += @$split[2];
}
$seconds = $this->sec % 60;
$minutes = $this->sec / 60;
$minutes = (integer)$minutes;
$minutes += $this->min;
$hours = $minutes / 60;
$minutes = $minutes % 60;
$hours = (integer)$hours;
$hours += $this->hou % 24;
$this->totaltime = $hours.":".$minutes.":".$seconds;
}
}
public function get_total_time(){
return $this->totaltime;
}
}
// Your array
$times = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
$counter = new times_counter($times);
echo $counter->get_total_time();
//outputs:
// 0:20:28
答案 4 :(得分:0)
我相信它是与你试图实现的逻辑分离的任务并将其包装为辅助(或转换器)类,代码是自我解释的:
class Helper_TimeAggregator {
private $_durations = null;
private $_seconds = null;
/**
* Constructor with init & constructor by default
*/
public function __construct($durations = null) {
if (isset($durations)) {
$this->setDurations($durations);
}
}
/**
* Setter for init
*/
public function setDurations($newDurations) {
$this->_durations = $newDurations;
$this->_seconds = null;
}
/**
* Returns total seconds
*/
public function getTotalSeconds() {
if (!isset($this->_seconds)) {
if (!is_array($this->_durations)) {
return '00:00:00';
}
$seconds = 0;
foreach ($this->_durations as $duration) {
$parts = explode(':', $duration);
if (3 === count($parts)) {
$seconds += $parts[0] * 3600;
$seconds += $parts[1] * 60;
$seconds += $parts[2];
}
}
$this->_seconds = $seconds;
}
return $this->_seconds;
}
/**
* Converts to data
*/
public function getTotalTime() {
return date("H:i:s", $this->getTotalSeconds());
}
/**
* Using default method by default
*/
public function __toString() {
return $this->getTotalTime();
}
}
使用示例:
$durations = array(
'00:05:34',
'00:03:26',
'00:06:13',
'00:05:15'
);
$t = new Helper_TimeAggregator($durations);
echo $t;
欢迎所有改进!