Jump to content

Convert Playtime in ms to DD:HH:MM


'LinKin

Recommended Posts

Posted

Hello,

I've been told MTA has a function to output the time format from ms (Obtained by getTickCount()).

However I canno't find it

How to turn those ms into Days:Hours:Mins format?

Also, how can I refresh that time each minute that the player is in the server? Maybe a timer in a client-side script?

Thanks.

Need a clanwar script? Click here!

Do you want some free scripts for your DD server? Visit my website.

Posted

You can use the below code to format milliseconds to days, hours, minutes and seconds.

function formatMilliseconds(milliseconds) 
    local totalseconds = math.floor( milliseconds / 1000 ) 
    milliseconds = milliseconds % 1000 
    local seconds = totalseconds % 60 
    local minutes = math.floor( totalseconds / 60 ) 
    local hours = math.floor( minutes / 60 ) 
    local days = math.floor( hours / 24 ) 
    minutes = minutes % 60 
    hours = hours % 24 
    return string.format( "%03d:%02d:%02d:%02d:%03d", days, hours, minutes, seconds, milliseconds )   
end 

You can use a timer so, that it goes through all the players at once, so that you won't have to define a timer on each player. Of course making it client-side would reduce any server-side lag, so I suppose you can use a timer. You can also use getTickCount do that, so it's up to you.

If I helped you, please click the like button on the right ;) Thanks!

Posted

Thanks,

I made this code by myself, it does the same thing (format ms)

addCommandHandler("msform",  
function () 
local days = 0 
local hours = 0 
local mins = 0 
local secs = 0 
local playTime = --getSQLDatum() 
  
while playTime >= 1000 do 
    if playTime >= 86400000 then 
        days = days + 1 
        playTime = playTime - 86400000 
    elseif playTime >= 3600000 then 
        hours = hours + 1 
        playTime = playTime - 3600000 
    elseif playTime >= 60000 then 
        mins = mins + 1 
        playTime = playTime - 60000 
    elseif playTime >= 1000 then 
        secs = secs + 1 
        playTime = playTime - 1000 
    end 
end 
outputChatBox(days.."D, "..hours.."H, "..mins.."M, "..secs.."S.") 
end) 

But I still don't really know how to refresh/update the time spent on server for a player each passed minute.

I'm thinking about making a function that calls itself; for example, client-side (For avoiding a table containing timers and some other stuff)

  
addEvent("onClientPlayerLogin", true) -- I will trigger this event with onPlayerLogin in the server-side file. 
function startTimer() 
  setTimer(checkTimePlayed, 60000, 1) 
  loginTime = getTickCount() 
end 
addEventHandler("onClientPlayerLogin", root, startTimer) 
  
function checkTimePlayed () 
  local currentTime = getTickCount() 
  local playTime = -- I will manage to get the playtime stored in the SQL db 
  local newTime = currentTime - loginTime 
  local finalTime = playTime + newTime 
  -- updateSQL (I will call a server function to update the SQL column with the new value (finalTime)) 
  -- This is what I mean; to set a timer that calls the same function each minute. 
  setTimer(checkTimePlayed, 60000, 1) 
end 
  

Would that work?

Need a clanwar script? Click here!

Do you want some free scripts for your DD server? Visit my website.

Posted

If that timer triggers that function every 1 minute, you could just add 1 minute, or 60000 ms to playtime instead of calculating anything.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...