Jump to content


Photo

Convert Seconds into Hours and Minutes


  • Please log in to reply
4 replies to this topic

#1 flOid

flOid
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 11 April 2006 - 09:00 PM

I have written a small script which outputs the total number of seconds of a MP3 playlist stored in a database. But I would like to output total hours and minutes of the playlist like "the playlist has a running time of xx hours and xx minutes".

I'm a PHP newbie and bad in maths, so could anyone provide me the needed algo?

#2 Twentyoneth

Twentyoneth
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationGlasgow, KY

Posted 11 April 2006 - 09:05 PM

$totaltime = 360 (seconds)
$hours = (($totaltime / 60) / 60);

echo $hours;



That should give you a decimal point number, like 0.something hours....that would be my best try :S

#3 flOid

flOid
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 11 April 2006 - 09:27 PM

[!--quoteo(post=363795:date=Apr 11 2006, 04:05 PM:name=Twentyoneth)--][div class=\'quotetop\']QUOTE(Twentyoneth @ Apr 11 2006, 04:05 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$totaltime = 360 (seconds)
$hours = (($totaltime / 60) / 60);

echo $hours;
That should give you a decimal point number, like 0.something hours....that would be my best try :S
[/quote]

thanx, but that was not what I was looking for, I actually could have done that by myself. What I was looking for was not a decimal number but seperated values for total hours and minutes.


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 09:49 PM

Here is function I wrote a while back... It should help you with your problem.
<?php
function time_diff($in_date)
{
    $minute = 60;
    $hour = 60 * 60;
    $day = $hour * 24;
    $now = time();
    $start = strtotime($in_date);
    $seconds_left = $now - $start;
    $days_left = floor($seconds_left / $day);
    $hours_left = floor(($seconds_left % $day)/$hour);
    $minutes_left = floor((($seconds_left % $day) % $hour) / $minute);
    $seconds = $seconds_left - ($days_left * $day) - ($hours_left * $hour) - ($minutes_left * $minute);
    return (array($days_left,$hours_left,$minutes_left,$seconds));
}

echo '<pre>' . print_r(time_diff('2006-01-01'),true) . '</pre>';


?>

Ken

#5 flOid

flOid
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 12 April 2006 - 01:10 AM

[!--quoteo(post=363812:date=Apr 11 2006, 04:49 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 04:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here is function I wrote a while back... It should help you with your problem.
<?php
function time_diff($in_date)
{
    $minute = 60;
    $hour = 60 * 60;
    $day = $hour * 24;
    $now = time();
    $start = strtotime($in_date);
    $seconds_left = $now - $start;
    $days_left = floor($seconds_left / $day);
    $hours_left = floor(($seconds_left % $day)/$hour);
    $minutes_left = floor((($seconds_left % $day) % $hour) / $minute);
    $seconds = $seconds_left - ($days_left * $day) - ($hours_left * $hour) - ($minutes_left * $minute);
    return (array($days_left,$hours_left,$minutes_left,$seconds));
}

echo '<pre>' . print_r(time_diff('2006-01-01'),true) . '</pre>';
?>

Ken
[/quote]

Indeed that was very helpful! My script is working now, thanx a lot! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users