我希望通过此链接http://gdata.youtube.com/feeds/api/playlists/PLCK7NnIZXn7gGU5wDy9iKOK6T2fwtGL6l
将所有视频时间与youtube播放列表相提并论。这里有时间代码,如time='00:05:11.500'
..我想从php获取所有视频时间,然后从php这样的节目
show it like this : 2:10:50 (2=hours,10=minutes,50=seconds)
我希望从这个变量来自php。 plzz帮助这篇文章谢谢。我试图这样做..但我可以做到这一点..如果有人可以帮助我..如果有4个视频,想要等于所有视频时间,然后想要显示所有持续时间只有PHP
答案 0 :(得分:6)
好的,这是一个解决问题的答案,假设你没有任何代码,也没有意图 尝试自己做实验。
除了描述的完全问题之外,您可能无法使用此功能: 将此Feed的所有持续时间加在一起并将其显示为小时:分钟:秒
<?php
$total_seconds = 0;
$dom = new DOMDocument();
$dom->loadXML(file_get_contents('http://gdata.youtube.com/feeds/api/playlists/PLCK7NnIZXn7gGU5wDy9iKOK6T2fwtGL6l'));
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//yt:duration/@seconds') as $duration) {
$total_seconds += (int) $duration->value;
}
然后以格式显示$ total_seconds。这有两个选择:
assuming that hours will never be larger than 24
echo gmdate("H:i:s", $total_seconds);
allowing total time to be larger than 24 hours
echo (int) ($total_seconds / 3600) . ':' . (int) ($total_seconds / 60) % 60 . ':' . $total_seconds % 60;
请记住:此代码执行ZERO错误检查。可能出错的事情:
答案 1 :(得分:3)
修改强>
我仔细看了一下饲料,似乎&#34;时间&#34;条目只是缩略图的指针。视频的实际持续时间以秒<yt:duration seconds='667'/>
设置,因此您可以将它们作为整数一起添加,然后使用DateTime类转换为您的格式。示例here。
结束编辑
首先,为了获得所有时间,您可能需要PHP中的原子提要阅读器。有plenty out there。不要试图解析XML,ATOM是一个众所周知的标准,应该很容易使用(如果你真的只想要时间,你可以使用xpath查询)。
现在你可以随时使用,你需要一种方法来轻松添加它们,最好不要乱用嵌套循环和if语句。
这是一个代表单个视频的单个时间条目的类:
final class Duration
{
private $hours;
private $minutes;
private $seconds;
private $centis;
/* we don't want any Durations not created with a create function */
private function __construct() {}
public static function fromString($input = '00:00:00.000') {
$values = self::valuesFromString($input);
return self::fromValues($values['hours'], $values['minutes'], $values['seconds'], $values['centis']);
}
public function addString($string) {
$duration = self::fromString($string);
return $this->addDuration($duration);
}
public function addDuration(Duration $duration) {
// add the durations, and return a new duration;
$values = self::valuesFromString((string) $duration);
// adding logic here
$centis = $values['centis'] + $this->centis;
$this->fixValue($centis, 1000, $values['seconds']);
$seconds = $values['seconds'] + $this->seconds;
$this->fixValue($seconds, 60, $values['minutes']);
$minutes = $values['minutes'] + $this->minutes;
$this->fixValue($minutes, 60, $values['hours']);
$hours = $values['hours'] + $this->hours;
return self::fromValues($hours, $minutes, $seconds, $centis);
}
public function __toString() {
return str_pad($this->hours,2,'0',STR_PAD_LEFT) . ':'
. str_pad($this->minutes,2,'0',STR_PAD_LEFT) . ':'
. str_pad($this->seconds,2,'0',STR_PAD_LEFT) . '.'
. str_pad($this->centis,3,'0',STR_PAD_LEFT);
}
public function toValues() {
return self::valuesFromString($this);
}
private static function valuesFromString($input) {
if (1 !== preg_match('/(?<hours>[0-9]{2}):(?<minutes>([0-5]{1}[0-9]{1})):(?<seconds>[0-5]{1}[0-9]{1}).(?<centis>[0-9]{3})/', $input, $matches)) {
throw new InvalidArgumentException('Invalid input string (should be 01:00:00.000): ' . $input);
}
return array(
'hours' => (int) $matches['hours'],
'minutes' => (int) $matches['minutes'],
'seconds' => (int) $matches['seconds'],
'centis' => (int) $matches['centis']
);
}
private static function fromValues($hours = 0, $minutes = 0, $seconds = 0, $centis = 0) {
$duration = new Duration();
$duration->hours = $hours;
$duration->minutes = $minutes;
$duration->seconds = $seconds;
$duration->centis = $centis;
return $duration;
}
private function fixValue(&$input, $max, &$nextUp) {
if ($input >= $max) {
$input -= $max;
$nextUp += 1;
}
}
}
您只能通过调用静态工厂fromString()创建一个新的持续时间,并且只接受格式为&#34; 00:00:00.000&#34; (小时:分钟:seconds.milliseconds):
$duration = Duration::fromString('00:04:16.250');
接下来,您可以添加另一个字符串或实际持续时间对象,以创建新的持续时间:
$newDuration = $duration->addString('00:04:16.250');
$newDuration = $duration->addDuration($duration);
持续时间对象将以“00:00:00.000&#39;
:的格式输出自己的持续时间字符串echo $duration;
// Gives
00:04:16.250
或者,如果您对单独的值感兴趣,可以这样做:
print_r($duration->toValues());
// Gives
Array
(
[hours] => 0
[minutes] => 4
[seconds] => 16
[milliseconds] => 250
)
在循环中使用它来获取总视频时间的最终示例:
$allTimes = array(
'00:30:05:250',
'01:24:38:250',
'00:07:01:750'
);
$d = Duration::fromString();
foreach ($allTimes as $time) {
$d = $d->addString($time);
}
echo $d . "\n";
print_r($d->toValues());
// Gives
02:01:45.250
Array
(
[hours] => 2
[minutes] => 1
[seconds] => 45
[milliseconds] => 250
)
我是根据Mathias Veraes在"named constructors"上发表的博客文章为自己写的这个练习。
另外,我也无法拒绝添加他的"TestFrameworkInATweet":
function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);}
function throws($exp,Closure $cb){try{$cb();}catch(Exception $e){return $e instanceof $exp;}return false;}
it('should be an empty duration from string', Duration::fromString() == '00:00:00.000');
it('should throw an exception with invalid input string', throws("InvalidArgumentException", function () { Duration::fromString('invalid'); }));
it('should throw an exception with invalid seconds input string', throws("InvalidArgumentException", function () { Duration::fromString('00:00:61:000'); }));
it('should throw an exception with invalid minutes input string', throws("InvalidArgumentException", function () { Duration::fromString('00:61:00:000'); }));
it('should add milliseconds to seconds', Duration::fromString('00:00:00.999')->addString('00:00:00.002') == Duration::fromString('00:00:01.001'));
it('should add seconds to minutes', Duration::fromString('00:00:59.000')->addString('00:00:02.000') == Duration::fromString('00:01:01.000'));
it('should add minutes to hours', Duration::fromString('00:59:00.000')->addString('00:02:00.000') == Duration::fromString('01:01:00.000'));
it('should add all levels up', Duration::fromString('00:59:59.999')->addString('00:01:01.002') == Duration::fromString('01:01:01.001'));
$duration = Duration::fromString('00:00:01.500');
it('should add a Duration', $duration->addDuration($duration) == '00:00:03.000');