• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Function] time formatting(x days, 1 minute and x seconds left)

zbizu

Legendary OT User
Joined
Nov 22, 2010
Messages
3,323
Solutions
26
Reaction score
2,694
Location
Poland
I needed a function which could do proper time formatting, but I couldn't even find any examples so I wrote this one myself.
It simply converts seconds to text which can be used as remaining time.

Do not post that script on other forums without my permission

examples:
showTimeLeft(868381, true) -> 10 days, 1 hour, 13 minutes and 1 second left.
showTimeLeft(868381, false) -> 10:01:13:01

function:
If you are using other distribution than TFS 1.0 replace :split to :explode
Code:
function showTimeLeft(number, usewords)
   local number = tonumber(number)
   if not number then
     return "error"
   end

   if number < 0 then
     return "expired"
   end

   local clocknum = os.date("!%X",number):split(":") -- h:m:s
   local day = math.modf(number / 86400)
   local hour = clocknum[1]
   local minute = clocknum[2]
   local second = clocknum[3]

   if not usewords then
     return table.concat({day, hour, minute, second}, ":")
   end

   local text = {}
   if day > 0 then
     table.insert(text, tonumber(day) .. " day" .. (day > 1 and "s" or ""))
   end

   if hour ~= "00" then
     table.insert(text, tonumber(hour) .. " hour" .. (tonumber(hour) > 1 and "s" or ""))
   end

   if minute ~= "00" then
     table.insert(text, tonumber(minute) .. " minute" .. (tonumber(minute) > 1 and "s" or ""))
   end

   if second ~= "00" then
     table.insert(text, tonumber(second) .. " second" .. (tonumber(second) > 1 and "s" or ""))
   end

   countdown_text = ""
   if #text > 0 then
     countdown_text = text[1]
     for i = 2, #text - 1 do
       countdown_text = countdown_text .. ", " .. text[i]
     end
     if #text > 1 then
       countdown_text = countdown_text .. " and " .. text[#text]
     end
     countdown_text = countdown_text .. " left."
   else
     countdown_text = "expired"
   end
return countdown_text
end
 
Last edited:
Back
Top