Question

Possible Duplicate:
Convert seconds into days, hours, minutes, seconds in PHP
How to convert a decimal into time, eg. HH:MM:SS

what I am doing here is I am saving posts time as UTC in mysql, after retrieving the time stamp from sql I am using strtotime() to convert it into seconds, and then am adding offset time to these seconds to show current local time. Now the question here is when I try to reconvert these seconds into hours, say for example

$seconds = 330*60 //Thats 5.30 hours and 60 seconds gives me 19800 as an output

now when I reconvert it back to hours its showing me 5.50 instead of 5.30

sprintf ("%.2f", 19800/60/60); //Output of 5.50 instead I want is 5.30 

I know it's a mathematical expression am doing wrong, any help would be appreciated with a clean explanation.

Was it helpful?

Solution

Not the most elegant solution but this works:

$original = (19800 / 60 / 60);
$hours = floor($original);
$minutes = ($original - $hours) * 60;

Or this is probably better using modulus:

$hours = floor(19800 / 60 / 60);
$minutes = (19800 / 60) % 60;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top