• 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!

timeString(timeDiff) - "23 hours, 30 minutes and 10 seconds"

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Hey there ;)

I made this for a script :p It's very simple to add more date formats, currently its only days, hours, minutes and seconds.

UPDATED 2017-02-15

Place this function in data/lib/functions.lua:
Lua:
function timeString(timeDiff, roundVal, roundUpVal)
  if roundVal and roundVal <= timeDiff then
    timeDiff = timeDiff - (roundUpVal or 0)
    timeDiff = timeDiff - timeDiff % -roundVal
  end

  if timeDiff <= 0 then
    return "none"
  end

  local dateValues = {
      {"week", timeDiff / 60 / 60 / 7},
      {"day", timeDiff / 60 / 60 / 24},
      {"hour", timeDiff / 60 / 60 % 24},
      {"minute", timeDiff / 60 % 60},
      {"second", timeDiff % 60}
  }

  local result = {}
  for _, dateValue in ipairs(dateValues) do
    local value = math.floor(dateValue[2])
    if value > 0 then
      table.insert(result, ("%d %s%s"):format(value, dateValue[1], value ~= 1 and "s" or ""))
    end
  end

  local ret = table.concat(result, ", ", 1, math.max(1, #result - 1))
  if #result > 1 then
    ret = ("%s and %s"):format(ret, result[#result])
  end
  return ret
end

function mtimeString(timeDiff, ...)
  return timeString(timeDiff / 1000, ...)
end

If you want to add for example weeks, you add this in table:
Lua:
{"week", timeDiff / 60 / 60 / 24 / 7},

Also, because you added there " / 7" you need to edit the one below to " % 7":
Lua:
{"day", timeDiff / 60 / 60 / 24 % 7},

With this you can make like quest like this:
Lua:
-- Quest
player:setStorageValue(3434, os.time())

...

-- Another script
local returnTime = 5 * 24 * 60 * 60 -- 5 days

local timeLeft = player:getStorageValue(3434) + returnTime - os.time()

doPlayerSendTextMessage(cid, 22, "Please come back in " .. timeString(timeLeft) .. "!")

This will tell player "Please come back in 4 days, 9 hours, 20 minutes and 51 seconds!"


Update

After the update you may now also use mtimeString(mtimeDiff) which expects time difference in milliseconds.

2 new parameters have been added to support rounding, e.g. if you do not want to display seconds, you may write timeString(diff, 60), this means 60 seconds will show as "1 minute" while 61 seconds will show as "2 minutes".

If you want a more specific rounding, you set the second third parameter as desired, e.g. timeString(diff, 60, 20), with time being 61 it will now show "1 minute" and with diff as 80 it will still show "1 minute". However, at 81 seconds it will now show "2 minutes" instead! If you want it to round up at exactly :30 seconds you write 29 as the 3rd parameter.

Using 59 as 3rd parameter would make sure it only increases the minute once it's been fully reached.
 
Last edited:
bump pwn script hehe can shorten world uptime script and make it show seconds (without you having trouble):
Lua:
function onSay(cid, words, param, channel)
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Uptime: " .. timeString(getWorldUpTime()) .. ".")
	return true
end
 
There is the possibility that some event happens on the server 7 in 7 days, at a certain time? Example every Sunday at 13:15, a script will work without someone having to run it, such as Script Castle War, where the functions addEvent, to open and close the gates, for the 13:00 hour run script that starts to make the score 15 minutes to open the gates of Castle War Well if someone has only one basis for how to get the timer 7 for 7 days work would be a great help!



Sorry for my Bad English
 
good, another useful feature, gonna use it on my system
all hail king lolandus :D
just what I needed to complete my repsys
 
Last edited:
Minor fix to prevent " and 45 seconds" and alike.
Code:
function timeString(timeDiff)
	local dateFormat = {
		{"day", timeDiff / 60 / 60 / 24},
		{"hour", timeDiff / 60 / 60 % 24},
		{"minute", timeDiff / 60 % 60},
		{"second", timeDiff % 60}
	}

	local out = {}
	for k, t in ipairs(dateFormat) do
		local v = math.floor(t[2])
		if(v > 0) then
			table.insert(out, (k < #dateFormat and (#out > 0 and ', ' or '') or ' and ') .. v .. ' ' .. t[1] .. (v ~= 1 and 's' or ''))
		end
	end
[B][COLOR="Red"]	local ret = table.concat(out)
	if ret:len() < 16 and ret:find("second") then
		local a, b = ret:find(" and ")
		ret = ret:sub(b+1)
	end[/COLOR][/B]
	
	return [B][COLOR="Red"]ret[/COLOR][/B]
end
Note that it's just an ugly hack - there is probably an easier and better way to do this.
 
Last edited:
It rox, Cyko is proster. :thumbup:
 
Back
Top