Get duration
#1

Hello, i have this stock:

pawn Код:
stock GetDuration(time)
{
    new str[32];

    if (time < 0 || time == gettime())
    {
        format(str, sizeof(str), "Never");
        return str;
    }
    else if (time < 60)
        format(str, sizeof(str), "%d seconds", time);

    else if (time >= 0 && time < 60)
        format(str, sizeof(str), "%d seconds", time);

    else if (time >= 60 && time < 3600)
        format(str, sizeof(str), (time >= 120) ? ("%d minutes") : ("%d minute"), time / 60);

    else if (time >= 3600 && time < 86400)
        format(str, sizeof(str), (time >= 7200) ? ("%d hours") : ("%d hour"), time / 3600);

    else if (time >= 86400 && time < 2592000)
        format(str, sizeof(str), (time >= 172800) ? ("%d days") : ("%d day"), time / 86400);

    else if (time >= 2592000 && time < 31536000)
        format(str, sizeof(str), (time >= 5184000) ? ("%d months") : ("%d month"), time / 2592000);

    else if (time >= 31536000)
        format(str, sizeof(str), (time >= 63072000) ? ("%d years") : ("%d year"), time / 31536000);

    strcat(str, " ago");

    return str;
}
And i use in this way:

pawn Код:
new createDate;
    cache_get_value_int(0, "RegisterDate", createDate);
    new string[128];
    format(string, sizeof(string), "Creation: %s", GetDuration(gettime() - createDate));
    SendClientMessage(playerid, -1, string);
Basically, the GetDuration stock should return how much time passed since something. I used to check how much time passed since registration date of the player.

But ingame it says: Creation: 48 yeard ago. Wtf?

P.S The RegisterDate row saves CORRECTLY the registration time/date
Reply
#2

Bump.
Reply
#3

http://forum.sa-mp.com/showpost.php?...postcount=4585
Reply
#4

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Thanks, but how to include my player define: Player[playerid][Registerdate] in your snippet?

pawn Код:
print(ReturnTimelapse(gettime(), gettime() + Player[playerid][RegisterDate]));
?
Reply
#5

The function returns the time elapsed between two timestamps i.e. "start" and "till".

"start" should be smaller than "till".

So if you store timestamps in "Player[playerid][RegisterDate]" (basically if you store "gettime()" in it), you can simply do this:
PHP код:
printf("Registered: %s"ReturnTimelapse(Player[playerid][RegisterDate], gettime())); 
Reply
#6

In RegisterDate i just store normal date: Ex 20/08/2018 22:10
Reply
#7

Then you have to split the string for year, month, day, hour, minute and second. Once you got all of those, you'll have to convert them into a single timestamp using "datetime2stamp()" (from library: https://github.com/Crayder/Time-Conversion) and after that, you can use it in the function.

OR

You can simply store a timestamp in "Player[playerid][RegisterDate]" and use the way i did in here: http://forum.sa-mp.com/showpost.php?...91&postcount=5
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)