Jump to content

[SOLVED] Converting Seconds to minutes:seconds


tbare

Recommended Posts

I'm getting the length of a movie using ffmpeg, but that displays the seconds only in form of "197.733333"

divided by 60 to get minutes gives me "3.29555555"

 

question:

 

what's the best way to get this to display in the form of "3:30" ?

Link to comment
Share on other sites

Or if you don't mind using antiquated C style functions:

 

$a = 197.333333;
$minutes = floor($a/60);
printf("%d minutes\n", $minutes);
printf("%.0f seconds\n", $a - ($minutes * 60));

 

printf's rounding will go to the closest number.  You can use sprintf() if you want to store the result in a variable instead.

Link to comment
Share on other sites

firstly: thanks for the correction of 3:17 (it had been a long day of programming) :)

 

secondly: GingerRobot's answer worked.... until i had a file that had seconds less than 10...

 

to fix this, all i did was add:

<?php
if($seconds < 10){
$seconds = "0" . $seconds;
}
?>

 

so the total code was:

<?php
$duration = $movie->getDuration();
$minutes = floor($duration / 60);
$seconds = round($duration % 60,2);
if($seconds < 10){
$seconds = "0" . $seconds;
}
$duration = $minutes.':'.$seconds;
?>

 

then just included that file before i printed $duration...

 

Thanks for all the help guys!

Link to comment
Share on other sites

Ah yeah, sorry. Didn't think that one through completely. An alternative to the if statement would be the str_pad function:

 

<?php
$duration = $movie->getDuration();
$minutes = floor($duration / 60);
$seconds = round($duration % 60,2);
$duration = $minutes.':'.str_pad($seconds,2,'0',STR_PAD_LEFT);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.