0

I'm looking for js that does something similar to this: http://www.unitarium.com/time-calculator.

My song-lengths are in an array, not in form fields. And the result have to display hh:mm:ss when needed (or always).

This is the page I want to use this on: http://flamencopeko.net/songs.php.

Ole Sørensen
  • 349
  • 5
  • 19
  • 1
    What have you tried? Consider looking [at the timecalc.js file on the page _you_ linked to](http://www.unitarium.com/timecalc.js). If you're stuck trying, _then_ ask a question here, but _show_ your efforts – Elias Van Ootegem Aug 08 '13 at 08:06
  • 1
    Store (or calculate) the song lengths in pure seconds (eg. 63s for 1:03), add them up and recalculate minutes (or hours) from the result. – Nero Aug 08 '13 at 08:07

2 Answers2

1

Make a function to convert the format mm:ss to seconds, and one to convert seconds to the format hh:mm:ss, convert all values to seconds, add them together, and format the result:

function parseMS(s) {
    var parts = s.split(':');
    return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
}

function formatTwo(n) {
    return (n < 10 ? '0' : '') + n.toString();
}

function formatHMS(s) {
    var m = Math.floor(s / 60);
    s %= 60;
    var h = Math.floor(m / 60);
    m %= 60;
    return formatTwo(h) + ':' + formatTwo(m) + ':' + formatTwo(s);
}

var times = ['14:23', '11:08', '18:59'];

var sum = 0;
for (var i = 0; i < times.length; i++) sum += parseMS(times[i]);
var result = formatHMS(sum);

alert(result);

Demo: http://jsfiddle.net/u6B4g/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • That looks awesome. Testing now. Thanks a bunch! – Ole Sørensen Aug 08 '13 at 08:45
  • Works perfect. Trying to get the song-lengths into an array and then json it here http://flamencopeko.net/songs_lengths.php. Source: http://flamencopeko.net/songs_lengths.txt. – Ole Sørensen Aug 08 '13 at 10:24
  • I don't understand json. Also, I'm not even sure if this is a php array: ` $file) { $contents = file($file); $total = trim($contents[1]); echo ("
    " . $total . "
    "); } ?>` Is it?
    – Ole Sørensen Aug 09 '13 at 06:35
  • 1
    @OleSørensen: JSON is just a subset of Javascript object literal syntax, so it's easy to learn. That code does show the contents of a PHP array in the page, but it's not JSON, and it's not in any format that Javascript code could easily use. I don't use PHP, but I think that I can modify it into something that would be at least close to usable: ` $file) { $contents = file($file); $total = trim($contents[1]); echo ("'" . $total . "',"); } echo("];") ?>`. – Guffa Aug 09 '13 at 07:58
  • Tested your code here: http://flamencopeko.net/songs_lengths_guffa.php. Array looks different, more like js, like you said. Also we got: **Total playing-time: 00:44:30**! That could very well be the right number for those 78 songs. So did you really find a way to transfer a php array to js without json? I thought that was impossible. However when I remove or add files, or change lengths it still says 00:44:30. So what is that number? This is very strange. I have a working json example here btw: http://flamencopeko.net/nameDump.php. But I'm not able to use it. – Ole Sørensen Aug 09 '13 at 17:49
  • Oh. Sorry. 00:44:30 is from time.js. – Ole Sørensen Aug 09 '13 at 18:27
  • @OleSørensen: The time `00:44:30` is from the data in my example code. You displayed the array from PHP in the page, but that doesn't make the Javascript use it. You should put the PHP code that creates the array inside the Javascript code instead of the `var times = ...` declaration already there. – Guffa Aug 09 '13 at 21:48
  • Yeah. Thanks. I've tried that and all kinds of stuff. I've just never successfully managed to get js to use the array from php. Not with Jquery either. – Ole Sørensen Aug 10 '13 at 11:09
  • Try that again. Look for messages in the error console to find out what's wrong with the code. – Guffa Aug 10 '13 at 13:15
  • Is this php inside js thing phpjs? Site: http://phpjs.org. I'm reading a bit about it now. You did mean I should put php on line 18 in time.js, yes? – Ole Sørensen Aug 10 '13 at 18:15
  • 1
    If you have it in an included file then the PHP code won't be executed unless you change the extension to php, and also add the code to create the PHP array. Instead just put the code that declares the Javascript array in the main page. – Guffa Aug 10 '13 at 18:42
  • I'm linking to time.js from head, as I'll be using it on two pages or more. Is that what you mean I should not do? Thanks for hanging in there. – Ole Sørensen Aug 10 '13 at 19:16
  • 1
    You need to put the declaration of the times variable in the page where you have the PHP array, but the rest of the code can be included if you like. – Guffa Aug 11 '13 at 07:44
  • Ok. Trying that. The js file should then look like this: http://flamencopeko.net/time3.js ? – Ole Sørensen Aug 12 '13 at 00:34
  • I'm still stuck on this one. – Ole Sørensen Sep 28 '13 at 18:56
  • You have to put the code in the correct order: 1. Create the array. 2. Calculate the sum. 3. Display the sum. – Guffa Sep 28 '13 at 19:19
0

I was able to use this PHP:

$str_time = "2:50";

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;

from Convert time in HH:MM:SS format to seconds only? to make bars for my song-lengths here: http://flamencopeko.net/songs.php.

I'll try to use that to get total playing time now.

I think array_sum() is the way to go, but I don't know how.

Up to date source: http://flamencopeko.net/covers.txt.

Community
  • 1
  • 1
Ole Sørensen
  • 349
  • 5
  • 19