简体   繁体   English

如何将“ 00:04:13.67”格式的时间转换为秒?

[英]How do I convert time in the format '00:04:13.67' to seconds?

basically i have a variable that holds a time value, in this format '00:04:13.67'. 基本上,我有一个保存时间值的变量,格式为'00:04:13.67'。 I need a short and simple php function to convert that to seconds. 我需要一个简短的php函数将其转换为秒。

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. 我最终想要做的是使用ffmpeg获取我存储在亚马逊云上的视频的时长,但是ffmpeg以不需要的格式“ hours:minutes:seconds.decimals”返回时长,我需要时间以秒为单位。

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];
<?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. $timeStr是您的时间字符串。 That should give you seconds (as float). 那应该给你几秒钟(浮动)。 Wrap it up in a function if you will. 如果需要,可以将其包装在一个函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM