os.time() arguments and os.difftime()

I’m working on a project and I’m trying to create a countdown timer, in days, taking today’s time and putting it in a MySQL database, and in 30 days it will run a function.

The documentation on os.time() is confusing, but essentially I want to find a way to get os.difftime() to return the difference between two times in days, so I can check if they are > 30.

Does anyone know how this would be done?

try


print((Time1-Time2)/86400)

Simple use the unix time, 30 days equals 302460*60

so if I set it to save os.time() at one instance, and then 30 days later this code is ran
[lua]
if ((os.time() - time)*86400 > 30 then return true end[/lua]
it would return true?

time - os.time() >= 86400

What is the variable ‘time’?
[lua]
local EndDate = (os.time() + 86400) --Current time(in seconds) plus one month(in seconds).
[/lua]
Just put the EndDate variable in your database. If you want to check if it is past or still before that date in time just find the difference.
[lua]
if os.time() > EndDate then
–30 days has passed.
else
–It hasn’t been 30 days yet.
local TimeLeft = ( EndDate - os.time() ) --This will tell you how much time(in seconds) is left.
local TimePassed = ( 86400 - TimeLeft ) --This is how much time has passed.
end
[/lua]

time is the date it starts, so why would i do time - os.time()?

wouldn’t that give me a negative?

-snip-

[editline]9/15/2012[/editline] I was bored so I wrote this:

Maybe it’ll help with something. :L

[lua]local Debug = true;

function ConvertTime( t_Time, t_From, t_To )

local t_Conversions	=
{
	["Seconds"] = {
		["Minutes"]	= t_Time / 60,
		["Hours"]	= t_Time / 3600,
		["Days"]	= t_Time / 86400,
		["Months"]	= t_Time / 2628000,
		["Years"]	= t_Time / 31536000,
	},
	
	["Minutes"] = {
		["Seconds"]	= t_Time * 60,
		["Hours"]	= t_Time / 60,
		["Days"]	= t_Time / 1440,
		["Months"]	= t_Time / 43800,
		["Years"]	= t_Time / 525600
	},
	
	["Hours"]	= {
		["Seconds"]	= t_Time * 3600,
		["Minutes"]	= t_Time * 60,
		["Days"]	= t_Time / 24,
		["Months"]	= t_Time / 730,
		["Years"]	= t_Time / 8760
	},
	
	["Days"]		= {
		["Seconds"]	= t_Time * 86400,
		["Minutes"]	= t_Time * 1440,
		["Hours"]	= t_Time * 24,
		["Months"]	= t_Time / 30.4166666667,
		["Years"]	= t_Time / 365
	},
	
	["Months"]	= {
		["Seconds"]	= t_Time * 2628000,
		["Minutes"]	= t_Time * 43800,
		["Hours"]	= t_Time * 730,
		["Days"]	= t_Time * 30.4166666667,
		["Years"]	= t_Time / 12
	},
	
	["Years"]	= {
		["Seconds"]	= t_Time * 31536000,
		["Minutes"]	= t_Time * 525600,
		["Hours"]	= t_Time * 8760,
		["Days"]	= t_Time * 365,
		["Months"]	= t_Time * 12
	};
};

----
-- math.Round( 10000 * Value ) / 10000;
--
--	The above is used to trim down a number.
--
--	From: 38.381666683
--	To: 38.3816
----

if ( Debug ) then
	print( "Converting \"" .. t_Time .. "\" from \"" .. t_From .. "\" to \"" .. t_To .. "\"." );
end

return math.Round( 10000 * t_Conversions[ t_From or "Minute" ] [ t_To or "Hour" ] ) / 10000;

end[/lua]

math.Round can take a second argument for the number of digits after the decimal point

Eg, math.Round( 1.1234, 2 ) will round to 1.12

Ooh I didn’t know that, The reason why I did it that way is because I had a project in Java where I was debugging the position of something, and the method in java doesn’t have a second argument.

You can also probably tell I’m used to other languages, since I use semi-colons and always use brackets around everything. (Also the ‘t_’ prefix to my variables stands for Temporary, because I didn’t like the way “tVariable” or “Variable” looked as a variable name.)