SA-MP Forums Archive
Is it possible to define time? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Is it possible to define time? (/showthread.php?tid=485782)



Is it possible to define time? - K9IsGodly - 05.01.2014

Title basically says it all, could I define like a second, minute, hour, day, month, year, etc? Then somehow incorporate that into a command that shows the date? Any help would be appreciated.


Re: Is it possible to define time? - Patrick - 05.01.2014

You mean this? I created this function before, or you could simply use getdate(..) and gettime(..)

pawn Код:
stock GetTodaysDate()
{
    new
        dString[ 18+2 ],
        Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);

    format(dString, sizeof (dString), "%d/%d/%d %d:%d:%d", Date[ 2 ], Date[ 1 ], Date[ 0 ], Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    return dString;
}
Original Thread: http://forum.sa-mp.com/showpost.php?...&postcount=255


Re: Is it possible to define time? - K9IsGodly - 05.01.2014

Kind of like this, but how could I make this right here into a /time command using ZCMD?


Re: Is it possible to define time? - Patrick - 05.01.2014

Quote:
Originally Posted by K9IsGodly
Посмотреть сообщение
Kind of like this, but how could I make this right here into a /time command using ZCMD?
This should work.

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Time: %i:%i", Date[3], Date[4]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    return true;
}



Re: Is it possible to define time? - K9IsGodly - 05.01.2014

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
This should work.

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Time: %i:%i", Date[3], Date[4]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Works perfectly, thanks man! One question though, is it possible for it to display the time like how for me right now it's 3:41 P.M. versus displaying it as 15:41 P.M.? So I guess what I mean is is there a way to have it print like 3:41 P.M. or if it was 3:41 A.M. it could detect that, too?


Re: Is it possible to define time? - Patrick - 05.01.2014

Quote:
Originally Posted by K9IsGodly
Посмотреть сообщение
Works perfectly, thanks man! One question though, is it possible for it to display the time like how for me right now it's 3:41 P.M. versus displaying it as 15:41 P.M.? So I guess what I mean is is there a way to have it print like 3:41 P.M. or if it was 3:41 A.M. it could detect that, too?
Nope, because gettime and getdate gets the information from the server side not from the client side, unless you host the server with your own computer then it will print the time that your computer currently have.


Re: Is it possible to define time? - Danyal - 05.01.2014

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 7 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    if(Date[ 3 ] >= 12 && Date[ 4 ] > 0)
    {
        Date [ 6 ] = Date[ 3 ] - 12;
        format( string, sizeof( string ), "Todays Time: %i:%i PM", Date[ 6 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    else if(Date[ 3 ] < 12 && Date[ 4 ] > 0)
    {
        format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    return true;
}
this should work the way you want...

EDIT: Modified PDS2k12's code a little


Re: Is it possible to define time? - KingHual - 05.01.2014

Quote:
Originally Posted by Danyal
Посмотреть сообщение
pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 7 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    if(Date[ 3 ] >= 12 && Date[ 4 ] > 0)
    {
        Date [ 6 ] = Date[ 3 ] - 12;
        format( string, sizeof( string ), "Todays Time: %i:%i PM", Date[ 6 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    else if(Date[ 3 ] < 12 && Date[ 4 ] > 0)
    {
        format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    return true;
}
this should work the way you want...

EDIT: Modified PDS2k12's code a little
A more compact version:

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ]%12, Date[ 4 ]), SendClientMessage( playerid, -1, string );
    return true;
}



Re: Is it possible to define time? - Danyal - 05.01.2014

Quote:
Originally Posted by king_hual
Посмотреть сообщение
A more compact version:

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ]%12, Date[ 4 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Your code will always show AM mate he still needs to switch statements with if condition


Re: Is it possible to define time? - KingHual - 05.01.2014

Quote:
Originally Posted by Danyal
Посмотреть сообщение
Your code will always show AM mate he still needs to switch statements with if condition
My bad,

Код:
format( string, sizeof( string ), "Todays Time: %i:%i %s", Date[ 3 ]%12, Date[ 4 ], (Date[3] >= 12 ? ("PM") : ("AM"))), SendClientMessage( playerid, -1, string );