Date+time+MySQL
#1

I use to save the time and date when player logs into his account in my server using NOW() function and mysql(the latest R34 plugin and include). It saves into the database properly, the column type is timestamp with Attributes on update CURRENT_TIMESTAMP and the saved timestamps look like: 2013-12-10 18:35:59 . So there are coming the problems, I tried to load the timestamp like this:
pawn Code:
cache_get_field_content(0, "Lastlogged", str),      PInfo[playerid][pLastLogin]] = strval(str);
//this is being send when player logs into his account
format(string, sizeof(string), "You last logged-in on: %s", PInfo[playerid][pLastLogin]);
SendClientMessage(playerid, -1, string);
And send it in a client message, but the message is empty i.e. it shows: You last logged-in on: so it doesn't show anything. I don't really see what is being wrong there.

Also a friend of mine, gave me this function:
pawn Code:
stock date( timestamp, _form=0 )
{
    /*
        ~ convert a Timestamp to a Date.
        ~ 10.07.2009

        date( 1247182451 )  will print >> 09.07.2009-23:34:11
        date( 1247182451, 1) will print >> 09/07/2009, 23:34:11
        date( 1247182451, 2) will print >> July 09, 2009, 23:34:11
        date( 1247182451, 3) will print >> 9 Jul 2009, 23:34
    */

    new year=1970, day=0, month=0, hour=0, mins=0, sec=0;

    new days_of_month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    new names_of_month[12][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    new returnstring[32];

    while(timestamp>31622400){
        timestamp -= 31536000;
        if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) ) timestamp -= 86400;
        year++;
    }

    if ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) )
        days_of_month[1] = 29;
    else
        days_of_month[1] = 28;


    while(timestamp>86400){
        timestamp -= 86400, day++;
        if(day==days_of_month[month]) day=0, month++;
    }

    while(timestamp>60){
        timestamp -= 60, mins++;
        if( mins == 60) mins=0, hour++;
    }

    sec=timestamp;

    switch( _form ){
        case 1: format(returnstring, 31, "%02d/%02d/%d %02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
        case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
        case 3: format(returnstring, 31, "%d %c%c%c %d, %02d:%02d", day+1,names_of_month[month][0],names_of_month[month][1],names_of_month[month][2], year,hour,mins);

        default: format(returnstring, 31, "%02d.%02d.%d-%02d:%02d:%02d", day+1, month+1, year, hour, mins, sec);
    }

    return returnstring;
}
So can you give me an example how to properly load the timestamp and also convert the loaded timestamps using the function above, I want to use:
pawn Code:
case 2: format(returnstring, 31, "%s %02d, %d, %02d:%02d:%02d", names_of_month[month],day+1,year, hour, mins, sec);
Reply
#2

Bump after 30 hours.
Reply
#3

Quote:
Originally Posted by x96664
View Post
pawn Code:
format(string, sizeof(string), "You last logged-in on: %s", PInfo[playerid][pLastLogin]);
Shouldn't there be %d in place of %s because it's not a string.

And what I think it should be is this

pawn Code:
date(pInfo[playerid][pLastLogin],1);
Reply
#4

Quote:
Originally Posted by Tayab
View Post
Shouldn't there be %d in place of %s because it's not a string.

And what I think it should be is this

pawn Code:
date(pInfo[playerid][pLastLogin],1);
Well, didn't help. Any other ideas guys ? Also how to convert the timestamp with that stock ?
Reply
#5

pawn Code:
cache_get_field_content(0, "Lastlogged", str),      PInfo[playerid][pLastLogin]] = strval(str);
As for this part, have you tried loading it as a string?

If you would like to use the date function, you will need to save it using a unix timestamp with gettime()
Then when you want to show it to a player you would send it like this:
pawn Code:
format(string, sizeof(string), "You last logged-in on: %s", date(PInfo[playerid][pLastLogin], 2));
SendClientMessage(playerid, -1, string);
Reply
#6

Quote:
Originally Posted by s0nic
View Post
pawn Code:
cache_get_field_content(0, "Lastlogged", str),      PInfo[playerid][pLastLogin]] = strval(str);
As for this part, have you tried loading it as a string?

If you would like to use the date function, you will need to save it using a unix timestamp with gettime()
Then when you want to show it to a player you would send it like this:
pawn Code:
format(string, sizeof(string), "You last logged-in on: %s", date(PInfo[playerid][pLastLogin], 2));
SendClientMessage(playerid, -1, string);
Well I'm not sure if I got problem with the table structure or something else, but it shows: January 01, 1970, 00:33:33 everytime I login, while in the db it looks like: 2013-12-12 16:29:48
Reply
#7

Well ?
Reply
#8

Bump!
Reply
#9

Well if you took the time to try to find the issue and fix it yourself, you could have been ready to go by now..

Anyhow..if its like that in your db and you're trying to use the date function..you are doing it incorrectly.
The date function converts unix timestamps to the result you are looking for..
If your going to use the date function you can change the column type to a integer, save the field as a unix timestamp using gettime()
Ex:
PInfo[playerid][pLastLogin] = gettime();
Or if your currently saving it through a query, change NOW() to UNIX_TIMESTAMP()
Reply
#10

Did you try this directly?

pawn Code:
cache_get_field_content(0, "Lastlogged", str);
format(string, sizeof( string ), ""You last logged in: %s", str);
SendClientMessage(playerid, -1, str);
without using strval?
It will give you an idea about what to look for.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)