Unixtimestamp to Readable Date (Days)
#1

Hello.
I read ___'s Unix Timestamp tutorial.
And i finally make a Temporary VIP System using the Unix Timestamp.

However,
There is problems on it, I want to display a client showing how many days (or what date) will the VIP expired.

The timec function results 52.

- timec(gettime()-TheTimeStamp)

Here is the timestamp i'm trying to display into readable date (days)

Код:
1381201221
And this is how i made the days into unix timestamp

pawn Код:
new expired = gettime()+(days*86400);
Base on ___'s tutorial.

I heard converting it is pretty hard, leap years etc...
Reply
#2

I use the timec function for bans. Here's an example, I use a slightly modified version of it so I only return the days left.

pawn Код:
format(query,sizeof(query),"SELECT * FROM `bans` WHERE `owner` = '%s' OR `ip` = '%s' AND`time` > '%d'",GetName(playerid),realIP[playerid],gettime());
    result = db_query(database,query);
    if(db_num_rows(result))
    {
        new id,name[24],ip[16];
        db_get_field_assoc(result,"id",ip,sizeof(ip)); id = strval(ip);
        db_get_field_assoc(result,"owner",name,sizeof(name));
        db_get_field_assoc(result,"ip",ip,sizeof(ip));
        db_get_field_assoc(result,"reason",reason,sizeof(reason));
        db_get_field_assoc(result,"time",string,sizeof(string)); time = strval(string);
        format(string,sizeof(string),"You're banned.\n\nBan ID: %d\nBanned name: %s\nBanned IP: %s\nBan expires: %s",id,name,ip,timec(time));
        ShowPlayerDialog(playerid,1,DIALOG_STYLE_MSGBOX,"Banned..",string,"Close","");
        KickEx(playerid);
        return db_free_result(result);
    }

stock timec(timestamp,compare = -1)
{
    if (compare == -1)
    {
        compare = gettime();
    }
    new n,Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,returnstr[32];
    if (d < 60)
    {
        format(returnstr,sizeof(returnstr),"< 1 minute");
        return returnstr;
    }
    else if (d < 3600)
    { // 3600 = 1 hour
        n = floatround(floatdiv(d,60.0),floatround_floor);
        format(returnstr,sizeof(returnstr),"minute");
    }
    else if (d < 86400)
    { // 86400 = 1 day
        n = floatround(floatdiv(d,3600.0),floatround_floor);
        format(returnstr,sizeof(returnstr),"hour");
    }
    else
    { // 2592000 = 1 month
        n = floatround(floatdiv(d,86400.0),floatround_floor);
        format(returnstr,sizeof(returnstr),"day");
    }
    if (n == 1)
    {
        format(returnstr,sizeof(returnstr),"1 %s",returnstr);
    }
    else
    {
        format(returnstr,sizeof(returnstr),"%d %ss",n,returnstr);
    }
    return returnstr;
}
In your case, you could use something like:


pawn Код:
CMD:vip(playerid,params[])
{
    new vip = GetPVarInt(playerid,"VIP"),string[124];
    if(vip < gettime()) format(string,sizeof(string),"You're not a vip.");
    else format(string,sizeof(string),"You're vip for the next %s",timec(vip));
    SendClientMessage(playerid,-1,string);
    return 1;
}
Reply
#3

Seems like, It returns wrong

I input

"/setvip Jake_Hero 1 3"

- /setvip PlayerID/Name Days Level.

It saves fine. However, timec displays, You're VIP for: 50 days. It should display, You're VIP for: 1 days.

pawn Код:
new expired = gettime()+(days*86400);
Again, this is the code i use to get the unix timestamp.
Reply
#4

Sorry but BUMP.
Reply
#5

anyone? It has been weeks now.
Reply
#6

Let me bump this thread I had 3 years ago, I am needing this on my RP script.

Making a unix timestamp readable ; E.G. - Expiration date @ September 2, 2016
Reply
#7

Let me ask you a question: are you using MySQL? Because if that's the case it's very simple and you can use the inbuilt FROM_UNIXTIME() function. Converting a timestamp using Pawn code is a royal pain in the ass.
Reply
#8

Nah I don't use MySQL, I'm using SQLite.. (I am having a hard time working with MySQL)
Reply
#9

Use the CTime plugin, No matter how good you write the function to do the change you will probably end up with a date off a day or two.
Reply
#10

I will check it out, Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)