Jump to content


Photo

Seconds to Minutes


  • Please log in to reply
8 replies to this topic

#1 stupidfly

stupidfly
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 March 2006 - 06:00 AM

I know how to do this in Java, but it seems to be different in PHP.

I have a given number of seconds. Say 140. I want to display it as 2:20.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 March 2006 - 08:23 AM

try
$sec = 140;

$min = floor($sec/60);
$sec = $sec % 60;

printf ('%0d:%02d', $min, $sec);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 stupidfly

stupidfly
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 March 2006 - 08:17 PM

Well it is truncating the tenths and hundreths of a second. I want to be able to display the decimals.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 March 2006 - 08:28 PM

Where are the tenths and hundredths in "140"?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 stupidfly

stupidfly
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 March 2006 - 08:35 PM

There are none, that was just an example. Use 101.11 as an example then. I need it to at least go to hundreths.

Thanks

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 March 2006 - 08:45 PM

As an example it wasn't very representative. Now I have to answer your question twice :-/

$secs = 101.11;

$mins = floor($secs/60);

$secs -= $mins*60;

printf ('%0d:%02.2f', $mins, $secs);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 stupidfly

stupidfly
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 March 2006 - 09:09 PM

Well it is working, but when the seconds are less than ten, it doesn't display the zero. For 4:04.86, it displays 4:4.86.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 04 March 2006 - 09:20 PM

Odd. I tested with

$secs = 120.11;

$mins = floor($secs/60);

$secs -= $mins*60;

printf ('%0d:%02.2f', $mins, $secs);

and got --> 2:00.11
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 stupidfly

stupidfly
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 04 March 2006 - 09:24 PM

Hmm... well thanks for your help. I'll just have to fiddle with it I guess.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users