Jump to content



Photo

Convert Video Duration Number to Time with PHP or jQuery


  • Please log in to reply
19 replies to this topic

#16 +Heartripper

Heartripper

    Neowinian

  • Joined: 10-December 09
  • Location: Italy
  • OS: Windows 8.1 / OSX 10.10
  • Phone: Nokia Lumia 925

Posted 10 October 2012 - 16:38

Haha it's okay. Yea, I'm still in a confusion on it here. I'm definitely going to need some help on that and maybe writing commas on the view count to make it a little more readable.


here it is:

function formatDuration(duration){
    //We only accept numbers
    if(typeof duration != "number"){
        return;
    }

    var pad = function(n){return n < 10 ? "0" + n : "" + n},
        timeComponents = [], multiplier = 3600;

    //Compute the value of each component (hours, minute, seconds)
    for(; multiplier != 0; multiplier = parseInt(multiplier / 60)){
        var component = parseInt(duration / multiplier);
        duration %= multiplier;

        //If hours equal to 0, do not add them.
        if(multiplier == 3600 && component == 0){
            continue;
        }

        timeComponents.push(component);
    }

    //Pad each component (2 -> 02)
    for(var i = 0; i < timeComponents.length; i++){
        var padded = pad(timeComponents[i]);
        timeComponents[i] = padded;
    }

    return timeComponents.join(":");
}

tell me if you want some tweaks. It converts seconds to hours:minutes:seconds format, e.g. 51823 -> 14:23:43


#17 Tjcool007

Tjcool007

    Neowinian

  • Joined: 27-April 11
  • Location: Belgium
  • OS: Windows 7 Pro x64, Ubuntu 13.04

Posted 11 October 2012 - 21:13

PHP version
<?php

function formatDuration($duration){
	$duration = intval($duration);
	if(!$duration) return;

	$result = array();
	if($duration >= 3600) {
		// Hours
		$result[] = str_pad(floor($duration / 3600),2,"0",STR_PAD_LEFT);
		$duration = $duration % 3600;
	}
	$result[] = str_pad(floor($duration / 60),2,"0",STR_PAD_LEFT);
	$result[] = str_pad($duration % 60,2,"0",STR_PAD_LEFT);

	return implode($result, ':');
}


#18 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 11 October 2012 - 21:24

PHP version

<?php

function formatDuration($duration){
	$duration = intval($duration);
	if(!$duration) return;

	$result = array();
	if($duration >= 3600) {
		// Hours
		$result[] = str_pad(floor($duration / 3600),2,"0",STR_PAD_LEFT);
		$duration = $duration % 3600;
	}
	$result[] = str_pad(floor($duration / 60),2,"0",STR_PAD_LEFT);
	$result[] = str_pad($duration % 60,2,"0",STR_PAD_LEFT);

	return implode($result, ':');
}


Won't that only work if I had a PHP version of the jQuery JSON function?

#19 Tjcool007

Tjcool007

    Neowinian

  • Joined: 27-April 11
  • Location: Belgium
  • OS: Windows 7 Pro x64, Ubuntu 13.04

Posted 11 October 2012 - 21:38

Yeah, I just saw PHP in the title xD
If you request the data through jQuery then yeah the PHP code will of course not work.

#20 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 October 2012 - 01:24

Haha, I try to have more than 10 of my JSON's in jQuery so loading would be more efficient.