• Time function
    8 replies, posted
how do i convert time to a string? i want to use it for a bansystem im making so people can see how long it till the person gets unbanned. something like TimetoUnban - os.time() and convert it to days and hours left couldt find anything about this on the wiki [B][URL="http://wiki.garrysmod.com/?title=Os.time"]Os.time [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
You could use Curtime but that might not work too well if the server crashed or restarted. EDIT: Wait wait, you could make a timer that get's all the bans every minute and subtracts 1 minute from the time. Then just save the ban times in just minutes. Then if you want to display it not just in minutes but also in weeks, days, hours, etc. you could just do some simple math to convert the time in minutes to the other forms. Hope that helps.
Taken from a post I did a while ago: [lua]local SECONDS_IN_A_MONTH = 2629743.83; local SECONDS_IN_A_DAY = 86400; local SECONDS_IN_A_HOUR = 3600; local SECONDS_IN_A_MINUTE = 60; local function FormatSeconds( s ) local mod; local months = math.floor( s / SECONDS_IN_A_MONTH ); mod = s % SECONDS_IN_A_MONTH; local days = math.floor( mod / SECONDS_IN_A_DAY ); mod = mod % SECONDS_IN_A_DAY; local hours = math.floor( mod / SECONDS_IN_A_HOUR ); mod = mod % SECONDS_IN_A_HOUR; local minutes = math.floor( mod / SECONDS_IN_A_MINUTE ); mod = mod % SECONDS_IN_A_MINUTE; local seconds = math.floor( mod ); return months.."months " .. days .. "days " .. hours .. "hours " .. minutes .. "minutes " .. seconds .. "seconds"; end[/lua] formats time in seconds into a string containing months, days, hours, minutes and seconds.
[QUOTE=BastinkaLive;21273276]You could use Curtime but that might not work too well if the server crashed or restarted. EDIT: Wait wait, you could make a timer that get's all the bans every minute and subtracts 1 minute from the time. Then just save the ban times in just minutes. Then if you want to display it not just in minutes but also in weeks, days, hours, etc. you could just do some simple math to convert the time in minutes to the other forms. Hope that helps.[/QUOTE] I think il take MakeR's code, but thank you any way [QUOTE=MakeR;21273402]Taken from a post I did a while ago: -some code- formats time in seconds into a string containing months, days, hours, minutes and seconds.[/QUOTE] Thank you MakeR for all help youve given to me Oh almost forgot, I have one more question do anyone know if lua os.time() and php time() gives the same time number at the same time? cause im making a web interface for my admin tool
It returns the time of day the server's operating system is currently.
-snip- never mind
[QUOTE=BastinkaLive;21274261]It returns the time of day the server's operating system is currently.[/QUOTE] It returns the number of seconds since the unix epoch. Yes, they will both return the same number (maybe a few seconds out, because they won't be called at the exact same time).
This might come in handy. [B][URL="http://wiki.garrysmod.com/?title=String.ToMinutesSeconds"]string.ToMinutesSeconds [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B]
Thank you Wintergrove but i think il stick to MakeR's function couse it works better with my purpose
Sorry, you need to Log In to post a reply to this thread.