1

I'm currently grabbing the length of a mp3 file from ffmpeg which results in a time such as 00:03:50.76 I'm needing to turn this into just total seconds without the milliseconds.

So the above would be 230 seconds. Would regex suffice for this?

user1372896
  • 542
  • 1
  • 10
  • 27
  • Almost, was having issues with the milliseconds however – user1372896 Jul 18 '15 at 16:13
  • This question, while old, is not actually a duplicate -- the linked SO post and solution do not cover handling time with milliseconds. https://stackoverflow.com/questions/4834202/convert-time-in-hhmmss-format-to-seconds-only – pipwerks Feb 21 '19 at 06:47

1 Answers1

0

Use this:

$res = "00:04:12.44";
$ex = explode(".", $res);
$res = $ex[0];
$secs = strtotime("01.01.1970 " . $res);

Or - see the comment of Sven:

$res = "00:03:50.76";
$ex = explode(".", $res);
$res = $ex[0];
$ex = explode(":", $res);
$secs = $ex[0] * 3600 + $ex[1] * 60 + $ex[2]; // 230 secs
Richard
  • 2,840
  • 3
  • 25
  • 37
  • This works, however it appears to be rounding the seconds to the nearest minute. Here is what I'm getting, seconds are on the left and the HH:MM:SS:MS I have are on the right https://dl.dropboxusercontent.com/u/4331988/ShareX/2015/07/2015-07-18_17-18-39.txt – user1372896 Jul 18 '15 at 16:19
  • Nevermind, $ex[0] should have been $ex[2]. Thank you! – user1372896 Jul 18 '15 at 16:22
  • Sorry :D I changed it - this solution is even easier. – Richard Jul 18 '15 at 16:25
  • 1
    This is a dangerous solution because the return value of `strtotime()` depends on the currently set timezone. Not setting this to UTC will produce wrong results. It will also not work for times of 25 hours or more. – Sven Jul 18 '15 at 18:29
  • @Sven: Thank you! I will add the other solution, too – Richard Jul 18 '15 at 18:43