Unix time help
#1

Hello SAMP'ers, so I've made a command, it's basic and I made it to know how gettime works, and to make VIP system in the future.

So, I've made this command, it's supposed to return "one month", not 46 years -.-, Why does it return 46 years? help Please!

Note:
2592000 = one month

PHP код:
CMD:expire(playeridparams[])
{
    new 
string[128];
    
pInfo[playerid][VIPExpire] = gettime()+2592000;
    
format(stringsizeof(string), ""COL_GRAY"V.I.P Expiry: "COL_WHITE"%s"timec(pInfo[playerid][VIPExpire]-gettime()));
    
SendServerMSG(playeridstring);
    return 
1;

Reply
#2

I've tried this, and still same shit.

PHP код:
CMD:expire(playeridparams[])
{
    new 
string[128], expr;
    
expr gettime()-2592000;
    
pInfo[playerid][VIPExpire] = gettime()-expr;
    
format(stringsizeof(string), ""COL_GRAY"V.I.P Expiry: "COL_WHITE"%s"timec(pInfo[playerid][VIPExpire]));
    
SendServerMSG(playeridstring);
    return 
1;

Reply
#3

Well how do we know? You haven't shown us your timec function...

Anyway, I made this function quite a while ago, so I suppose you could use that for this purpose too:
pawn Код:
stock ConvertTimeToString(weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0)
{
    while(seconds >= 60) minutes++, seconds -= 60;
    while(minutes >= 60) hours++, minutes -= 60;
    while(hours >= 24) days++, hours -= 24;
    while(days >= 7) weeks++, days -= 7;
    new string[70], fstr[20];
    if(weeks) { format(fstr, sizeof(fstr), (weeks == 1) ? ("%d week") : ("%d weeks"), weeks); if(days || hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, 0); }
    if(days) { format(fstr, sizeof(fstr), (days == 1) ? ("%d day") : ("%d days"), days); if(hours || minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(hours) { format(fstr, sizeof(fstr), (hours == 1) ? ("%d hour") : ("%d hours"), hours); if(minutes || seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(minutes) { format(fstr, sizeof(fstr), (minutes == 1) ? ("%d minute") : ("%d minutes"), minutes); if(seconds) strins(fstr, ", ", strlen(fstr)); strins(string, fstr, strlen(string)); }
    if(seconds) { format(fstr, sizeof(fstr), (seconds == 1) ? ("%d second") : ("%d seconds"), seconds), strins(string, fstr, strlen(string)); }
    return string;
}
Example:
pawn Код:
CMD:expire(playerid, params[])
{
    new string[128];
    pInfo[playerid][VIPExpire] = gettime() + 2592000;
    format(string, sizeof(string), ""COL_GRAY"V.I.P Expiry: "COL_WHITE"%s", ConvertTimeToString(.seconds = (pInfo[playerid][VIPExpire] - gettime())));
    SendServerMSG(playerid, string);
    return 1;
}
Though technically you could just convert '2592000' instead of doing the whole gettime() thing. You're effectively just doing:
Код:
gettime() + 2592000 - gettime() = 2592000
EDIT: Also I'm pretty sure I didn't make this use 'months' because of the 28 > 30 > 31 day differences, and because I was lazy. But that's a story for another time :>
Reply
#4

Bro, I want a simple usage, not that much hard e.e
Here's my timec function, copied from a tutorial in SAMP forums. Can you fix it please?
PHP код:
stock timec(timestampcompare = -1) {
    if (
compare == -1) {
        
compare gettime();
    }
    new
        
n,
        
// on the following line, I have removed the need for the diff() function.
        // if you want to use the diff() function in pawn, replace the following with:
        // Float:d = diff(timestamp, compare),
        
Float:= (timestamp compare) ? timestamp compare compare timestamp,
        
returnstr[32];
    if (
60) {
        
format(returnstrsizeof(returnstr), "less than a minute");
        return 
returnstr;
    } else if (
3600) { // 3600 = 1 hour
        
floatround(floatdiv(d60.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "minute");
    } else if (
86400) { // 86400 = 1 day
        
floatround(floatdiv(d3600.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "hour");
    } else if (
2592000) { // 2592000 = 1 month
        
floatround(floatdiv(d86400.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "day");
    } else if (
31536000) { // 31536000 = 1 year
        
floatround(floatdiv(d2592000.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "month");
    } else {
        
floatround(floatdiv(d31536000.0), floatround_floor);
        
format(returnstrsizeof(returnstr), "year");
    }
    if (
== 1) {
        
format(returnstrsizeof(returnstr), "1 %s"returnstr);
    } else {
        
format(returnstrsizeof(returnstr), "%d %ss"nreturnstr);
    }
    return 
returnstr;

Reply
#5

What do you mean? It doesn't get any easier... all you have to do is put in the amount of weeks, hours, minutes, and/or seconds and it will convert it for you... it's doesn't get any easier...

A timestamp is simply in seconds, so you convert the timestamp from seconds to weeks, days, hours, minutes and seconds in one 'simple' function.
Reply
#6

Hello, I have got a function, made by Threshold, it converts an amount of seconds to: seconds, minutes, hours, weeks. But, I want it to convert to days & months too. Please help.

Quote:
PHP код:
stock ConvertTimeToString(weeks 0days 0hours 0minutes 0seconds 0)
{
    while(
seconds >= 60minutes++, seconds -= 60;
    while(
minutes >= 60hours++, minutes -= 60;
    while(
hours >= 24days++, hours -= 24;
    while(
days >= 7weeks++, days -= 7;
    new 
string[70], fstr[20];
    if(
weeks) { format(fstrsizeof(fstr), (weeks == 1) ? ("%d week") : ("%d weeks"), weeks); if(days || hours || minutes || secondsstrins(fstr", "strlen(fstr)); strins(stringfstr0); }
    if(
days) { format(fstrsizeof(fstr), (days == 1) ? ("%d day") : ("%d days"), days); if(hours || minutes || secondsstrins(fstr", "strlen(fstr)); strins(stringfstrstrlen(string)); }
    if(
hours) { format(fstrsizeof(fstr), (hours == 1) ? ("%d hour") : ("%d hours"), hours); if(minutes || secondsstrins(fstr", "strlen(fstr)); strins(stringfstrstrlen(string)); }
    if(
minutes) { format(fstrsizeof(fstr), (minutes == 1) ? ("%d minute") : ("%d minutes"), minutes); if(secondsstrins(fstr", "strlen(fstr)); strins(stringfstrstrlen(string)); }
    if(
seconds) { format(fstrsizeof(fstr), (seconds == 1) ? ("%d second") : ("%d seconds"), seconds), strins(stringfstrstrlen(string)); }
    return 
string;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)