tags:

views:

36

answers:

1

basically i have a variable that holds a time value, in this format '00:04:13.67'. I need a short and simple php function to convert that to seconds.

What i'm ultimately tring to do is get the duration of videos i have stored on a amazon cloud front using ffmpeg, but ffmpeg returns duration in unwanted format "hours:minutes:seconds.decimals" i need time in seconds.

here's my code if any has a simpler or cleaner solution i'd appreciate it very much

$videofile="http://123abc.cloudfront.net/test.flv";
ob_start();
passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();

$duration=preg_match('/Duration: (.*?),/', $duration, $matches, PREG_OFFSET_CAPTURE, 3);
//TEST ECHO
echo $matches[1][0];
+1  A: 
<?php
$hours = (int) substr($timeStr, 0, 2);
$mins = (int) substr($timeStr, 3, 2) + $hours * 60;
$secs = (int) substr($timeStr, 6, 2) + $mins * 60;
$secs += ((int) substr($timeStr, 9, 2)) / 100;
?>

$timeStr is your time string. That should give you seconds (as float). Wrap it up in a function if you will.

Core Xii
'$secs = (int) substr($timeStr, 6, 2) + $mins * 60;' thats what i needed Thanks
dlaurent86
In film and video time codes, the number to the right of the decimal point is frames, not hundredths of a second.
Blrfl