59794

Converting MP3 duration time

I'm using my iTunes library to get data from about 1,100 mp3s and I'm running into a small issue in getting the duration of the library into minutes and seconds.

$duration = 1893642; $minutes = bcmod(($duration / 60), 60); $seconds = bcmod($duration, 60); echo $minutes.":".$seconds; //returns 0:42

The problem is that this specific MP3 is actually 31:42. Any thoughts on why this isn't working?

Answer1:

$minutes = bcmod(($duration / 60), 60);

is taking the minutes modulo 60. Unless your track is over an hour it will always say 0. You want it to be

$minutes = floor($duration / 60);

Answer2:

<strong>Try</strong> this function

function formatTime($secs) { $times = array(3600, 60, 1); $time = ''; $tmp = ''; for($i = 0; $i < 3; $i++) { $tmp = floor($secs / $times[$i]); if($tmp < 1) { $tmp = '00'; } elseif($tmp < 10) { $tmp = '0' . $tmp; } $time .= $tmp; if($i < 2) { $time .= ':'; } $secs = $secs % $times[$i]; } return $time; }

Answer3:

Not sure if the following function was available when this question was written, but as it's a question I've been asking myself so here goes.

I used the answer above:

$seconds = bcmod($row{'playtime_seconds'}, 60); $minutes = floor($row{'playtime_seconds'} / 60); $hours = floor($minutes / 60);

Which works for the majority of times, but there is no padding - so you can end up with 20:1 when it should be 20:01 - and it's not to good over an hour - one length comes in at length="1:70:9" - so an alternative is to use the "date" function.

<?=date("H:i:s", $duration); ?>

which returns <strong>00:31:42</strong> from that number of seconds

Recommend

  • `resolve` and `reject` functions inside a Promise's executor
  • How do you print a dollar sign $ in Dart
  • Is boxing coercion in JavaScript?
  • What is the meaning of the percent character in this code?
  • How to get the output from the comm command into 3 separate files?
  • Three-way xor-like function
  • android resolution need help
  • Alternative to readfile()
  • AVAudioPlayer doesn't load sound
  • Fastest way to decode a hexadecimal digit
  • Matrix Circular Shift
  • Change state's array without changing the whole state (REACT / REDUX)
  • Oracle SQL - Generate and update a column with different random number in every row
  • Allowing my app users to post tweets from a single account
  • Timer once a minute on the minute
  • 1º Day of Daylight Saving Time Java and JS showing a different behavior
  • How to crop a mp3 in ASP.NET + C#?
  • Timeout a query
  • Merge Module leaving files during uninstall
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Put value at centre of bins for histogram
  • Is it possible to define rest argument in OCaml?
  • HttpURLConnection.getOutputStream() takes 20 seconds. Why?
  • android google indoor map
  • java inputstream
  • Spring security and special characters
  • Date difference with leap year
  • How would I use PHP exceptions to define a redirect?
  • Excel - Autoshape get it's name from cell (value)
  • Running a C# exe file
  • Join two tables and save into third-sql
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Numpy divide by zero. Why?
  • Matrix multiplication with MKL
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • File not found error Google Drive API