tbare Posted December 6, 2007 Share Posted December 6, 2007 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" ? Quote Link to comment https://forums.phpfreaks.com/topic/80530-solved-converting-seconds-to-minutesseconds/ Share on other sites More sharing options...
GingerRobot Posted December 6, 2007 Share Posted December 6, 2007 Well, 197 seconds is actually 3 minutes and 17 seconds. Anyways, try: <?php function convert($seconds){ $minutes = floor($seconds / 60); $seconds = round($seconds % 60,2); return $minutes.':'.$seconds; } echo convert(197.733333); ?> Quote Link to comment https://forums.phpfreaks.com/topic/80530-solved-converting-seconds-to-minutesseconds/#findComment-408326 Share on other sites More sharing options...
btherl Posted December 7, 2007 Share Posted December 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/80530-solved-converting-seconds-to-minutesseconds/#findComment-408407 Share on other sites More sharing options...
tbare Posted December 7, 2007 Author Share Posted December 7, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/80530-solved-converting-seconds-to-minutesseconds/#findComment-408894 Share on other sites More sharing options...
GingerRobot Posted December 7, 2007 Share Posted December 7, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/80530-solved-converting-seconds-to-minutesseconds/#findComment-408924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.