Unix Time to Date

Started by vcmptr, Mar 13, 2015, 01:45 PM

Previous topic - Next topic

vcmptr

I need unix time to date and date to unix time. How can i do?
My English is not good.

Sk

you can use date() function.it returns a table with few index like day wday etc.
To use it do something like this
local t = time()+86400; // added one day.
local d = date(t).day; // day is name of index inside table date().
print("Today date is "+date().day+"Tomorrow the date will be "+d);
Note if you dont use any parameter/arguments for date() then the default time time() will be used.
and to know about all index in table date() you can use foreach like this.
local d = date();
foreach(index_name,value in d) print(index_name+" = "+value);
hope this helps.

.

#2
Usage is above. Specifications are bellow.

date([time], [format]);returns a table containing a date/time splitted in the slots:

sec Seconds after minute (0 - 59).
min Minutes after hour (0 - 59). 
hour Hours since midnight (0 - 23).
day Day of month (1 - 31). 
month Month (0 - 11; January = 0). 
year Year (current year). 
wday Day of week (0 - 6; Sunday = 0). 
yday Day of year (0 - 365; January 1 = 0). 

if time is omitted the current time is used.
if format can be 'l' local time or 'u' UTC time, if omitted is defaulted as 'l'(local time).


time();
returns the number of seconds elapsed since midnight 00:00:00, January 1, 1970.

the result of this function can be formatted through the function date().
.

vcmptr

Quote from: Sk on Mar 13, 2015, 03:06 PMyou can use date() function.it returns a table with few index like day wday etc.
To use it do something like this
local t = time()+86400; // added one day.
local d = date(t).day; // day is name of index inside table date().
print("Today date is "+date().day+"Tomorrow the date will be "+d);
Note if you dont use any parameter/arguments for date() then the default time time() will be used.
and to know about all index in table date() you can use foreach like this.
local d = date();
foreach(index_name,value in d) print(index_name+" = "+value);
hope this helps.
Quote from: S.L.C on Mar 13, 2015, 03:27 PMUsage is above. Specifications are bellow.

date([time], [format]);returns a table containing a date/time splitted in the slots:

sec Seconds after minute (0 - 59).
min Minutes after hour (0 - 59). 
hour Hours since midnight (0 - 23).
day Day of month (1 - 31). 
month Month (0 - 11; January = 0). 
year Year (current year). 
wday Day of week (0 - 6; Sunday = 0). 
yday Day of year (0 - 365; January 1 = 0). 

if time is omitted the current time is used.
if format can be 'l' local time or 'u' UTC time, if omitted is defaulted as 'l'(local time).


time();
returns the number of seconds elapsed since midnight 00:00:00, January 1, 1970.

the result of this function can be formatted through the function date().

No longer I will record times with time() funtion. Thanks! :)
My English is not good.

Sk

#4
Some time back i created some function to format that timestamps into Dersiable format. May be you can use some of them for your own server if you want.
On PasteBbin.com : http://pastebin.com/DmdD0fAG
On Bitbucket.org : https://bitbucket.org/snippets/SkNiaZi786/gMk5
Functions are :
GetTime(a = time()); returns a time in format HH:MM:SS for example 22:01:22.
GetDate(a = time()); returns a time in format DD-MM-YYYY for example 10-02-2015.
GetFullTime(a = time()); returns a time in Format of orignal GetFullTime() function from both 0.4 and 0.3 to get from 0.3 comment out last line of the function and remove comment from 2nd last line.
And The parameter in each function is optional (which is seconds of timestamps) so if you don't provide them then the default time() of current machine is used.
Some Examples
GetTime();
print(GetTime()); //This will print current time.
print(GetTime(time()+3600)); //This will print one hour after the current time.
GetDate();
print(GetDate()); //This will print current date.
print(GetDate(time()+86400)); //This will print one day after the current date.
GetFullTime();
print(GetTime()); //This will print time GetFullTime() format.
print(GetTime(time()+3600+86400)); //This will print one day and one hour after the time in GetFullTime() format .

and about your next question how to convert date into unix time.I hardly doubt if its even possible in squirrel because in php i use function strtotime() which is not present in squirrel :(. to get ride of this problem what you can do is don't save formatted time instead save unix time that way you won't need to convert is back to unix time.

vcmptr

#5
Hmm okay I will convert old records with PHP. Thanks. :)
My English is not good.