Converting seconds to hours and minutes
#1

I have used search and didn't found something that would really solve my problem. I'm converting seconds to hours and minutes like that:

pawn Code:
pData[playerid][p_online_time] / 3600 // hours
(pData[playerid][p_online_time / 60) % 60 // minutes
My problem is that it doesn't convert big values like 34000. It doesn't show anything.

Is there any other way that would work?
Reply
#2

if p_online_time is a value in seconds :
pawn Code:
//minutes
pData[playerid][p_online_time] / 60

//hours
pData[playerid][p_online_time] / 3600
Reply
#3

34000 / 3600 gives a decimal number. That should never happen and you should only ever get whole numbers.
pawn Code:
new totaltime = 34000; // this is your initial value, here for reference
new remaining; // temporary storage

remaining = totaltime % 3600; // remaining seconds after subtracting hours

new hours = (totaltime - remaining) / 3600;
new seconds = remaining % 60;
new minutes = (remaining - seconds) / 60;

// will give: 9 hours, 26 minutes, 40 seconds
Could probably be optimized a bit more, but that should work. Very important not to change the order of the statements.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)