Skip to content

Instantly share code, notes, and snippets.

@djonatanb
Created April 9, 2012 20:07
Show Gist options
  • Save djonatanb/2346246 to your computer and use it in GitHub Desktop.
Save djonatanb/2346246 to your computer and use it in GitHub Desktop.
Save with time
public function beforeSave($options = array())
{
foreach ($this->_youtube as $youtube) {
if (isset($this->data[$this->alias])
&& isset($this->data[$this->alias][$youtube])
) {
$matches = array();
if (preg_match(
self::REGEX, $this->data[$this->alias][$youtube], $matches
)) {
$this->data[$this->alias][$youtube] = reset($matches);
$xml = file_get_contents('http://gdata.youtube.com/feeds/api/videos/' . reset($matches) );
$search = preg_match("<yt:duration seconds=\'(.*?)\'\/>", $xml, $seconds);
$duration = $this->toMinutes($seconds[1]);
$this->data[$this->alias][$this->_duration[$youtube]] = $duration;
}
}
}
return parent::beforeSave($options);
}
public function toMinutes($seconds){
$floor = floor( $seconds / 60 );
return sprintf( "%02.2d:%02.2d:%02.2d", floor( $floor / 60 ), $floor, $seconds % 60 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment