[Tutorial] How to: Unix timestamps
#44

I've also used this timestamp feature for a temporary ban system and wrote a few things:
pawn Code:
// Convert Seconds to Days, Hours, Minutes and Seconds
StoDHMS(Time, &Days, &Hours, &Minutes, &Seconds)
{
    // Convert the given time in seconds to days, hours, minutes and seconds
    Days = Time / 86400;
    Time = Time - (Days * 86400);
    Hours = Time / 3600;
    Time = Time - (Hours * 3600);
    Minutes = Time / 60;
    Seconds = Time - (Minutes * 60);
}
You can supply this function with a given timestamp difference (to check how much time it still takes until the ban is automatically removed, and supply 4 variables in which the function returns it's calculations.
This function actually converts the amounts of seconds you supply into Days, Hours, Minutes and Seconds.

A small snippet from my current code show it in action:
pawn Code:
// Setup local variables
    new Msg[128], UnbanTime, RemainingBanTime, Days, Hours, Minutes, Seconds;

    // Check if this account is still banned
    UnbanTime = cache_get_field_content_int(0, "UnbanTime", SQL_db);
    if (UnbanTime > gettime())
    {
        // Get the remaining bantime (unbantime - current time)
        RemainingBanTime = UnbanTime - gettime();
        // Convert the remaining bantime to Days, Hours, Minutes and Seconds
        StoDHMS(RemainingBanTime, Days, Hours, Minutes, Seconds);
        // Kick the player
        format(Msg, sizeof(Msg), "{FF0000}Your account is still banned for %i days, %i hours, %i minutes and %i seconds", Days, Hours, Minutes, Seconds);
        Player_Kick(playerid, Msg);
        // Exit the function
        return 1;
    }
It reads the unbantime from the database (the timestamp when the ban will expire), substracts the current timestamp and the result is how long the ban remains in effect (in seconds).
The function converts it into Days, Hours, Minutes and Seconds and displays the remaining bantime to the player and kicks him.
Reply


Messages In This Thread
How to: Unix timestamps - by __ - 13.05.2011, 15:22
Re: How to: Unix timestamps - by CrunkBankS - 13.05.2011, 16:58
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:11
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:39
Re: How to: Unix timestamps - by [03]Garsino - 13.05.2011, 17:43
Re: How to: Unix timestamps - by __ - 13.05.2011, 17:58
Re: How to: Unix timestamps - by Zimon95 - 13.05.2011, 18:34
Re: How to: Unix timestamps - by __ - 14.05.2011, 10:10
Re: How to: Unix timestamps - by Haydz - 16.05.2011, 10:52
Re: How to: Unix timestamps - by linuxthefish - 16.05.2011, 11:40
Re: How to: Unix timestamps - by Swiftz - 16.05.2011, 15:47
Re: How to: Unix timestamps - by Zimon95 - 16.05.2011, 17:57
Re: How to: Unix timestamps - by __ - 17.05.2011, 08:39
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 00:23
Re: How to: Unix timestamps - by __ - 19.05.2011, 10:04
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 14:30
Re: How to: Unix timestamps - by __ - 19.05.2011, 14:42
Re: How to: Unix timestamps - by BaubaS - 19.05.2011, 16:38
Re: How to: Unix timestamps - by Blacklite - 19.05.2011, 22:00
Re: How to: Unix timestamps - by Scenario - 14.06.2011, 19:30
Re: How to: Unix timestamps - by Anzipane! - 14.06.2011, 20:39
Re: How to: Unix timestamps - by Blacklite - 14.06.2011, 21:14
Re: How to: Unix timestamps - by jot16 - 14.06.2011, 21:17
Re: How to: Unix timestamps - by Jack_Rocker - 14.06.2011, 21:20
Re: How to: Unix timestamps - by Iphone1234g - 15.06.2011, 09:41
Re: How to: Unix timestamps - by __ - 17.06.2011, 23:08
Re: How to: Unix timestamps - by Steamator - 20.06.2011, 14:45
Re: How to: Unix timestamps - by ||123|| - 14.07.2011, 07:26
Re: How to: Unix timestamps - by cessil - 14.07.2011, 12:50
Re: How to: Unix timestamps - by __ - 15.07.2011, 05:39
Re: How to: Unix timestamps - by Jantjuh - 16.07.2011, 19:49
Re: How to: Unix timestamps - by BrUn3S - 18.10.2011, 18:59
Re: How to: Unix timestamps - by Scenario - 17.12.2011, 00:28
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:02
Re: How to: Unix timestamps - by Scenario - 07.01.2012, 20:07
Re: How to: Unix timestamps - by Rokzlive - 07.01.2012, 20:14
Re: How to: Unix timestamps - by Calgon - 23.03.2012, 10:42
Re: How to: Unix timestamps - by __ - 15.06.2012, 07:19
Re: How to: Unix timestamps - by Tanush123 - 22.05.2013, 21:03
Re: How to: Unix timestamps - by trukker1998 - 19.02.2014, 11:35
Re: How to: Unix timestamps - by Scenario - 19.02.2014, 11:53
Re: How to: Unix timestamps - by AroseKhanNiazi - 01.03.2014, 11:38
Re: How to: Unix timestamps - by _Application_ - 20.01.2015, 11:20
Re: How to: Unix timestamps - by PowerPC603 - 20.01.2015, 13:37
Re: How to: Unix timestamps - by AdamCooper - 07.10.2018, 00:45

Forum Jump:


Users browsing this thread: 3 Guest(s)