SQL CURDATE() and Calculating Days Ahead
#1

Hi guys,

I'm back with yet another question for all you wonderful scripters to educate me with. So I am working on a system currently that I thought, 'Hey! I'll put a date into the database under this players character, and after two days, if they haven't paid the fines due from that date, then issue a warrant out on that player'.

So I went ahead and produced the system that detected when a player was to be given a fine for speeding by the speed cameras. Then input the date into the database under their character, the date being the day they got issued their fine.

Using the query
PHP код:
            if(CharInfo[playerid][FineTotal] == 0)
            {
                
format(querysizeof(query), "UPDATE `pCharacters` SET FineDue=CURDATE() WHERE cID='%d'"CharInfo[playerid][cID]);
                
mysql_function_query(dbHandlequeryfalse"""");
            } 
Afterwards, it as expected, saved the date in the format of '2015-11-15'. Now obviously this is not my traditional date format, being from the UK we have a tendency to use DD/MM/YYYY however, date formats don't bother me too much as I'm good at working them out.

However, I'm trying to think of a way to tell players that it has been two days since they received their first fine and it should be paid now, so obviously, todays date being in this format 2015-11-15, they would obviously be notified on '2015-11-17'.

I ALWAYS do my best to give a solution of my own to work off as I am not one to expect people to do the work for me. However, the string for this date is 9 characters. Would it just be as simple as searching the string for the last 2 characters (character 8 and 9) and then converting that string to a value and adding two onto it? I hope that makes sense.

Thanks guys.
Reply
#2

Use either UNIX_TIMESTAMP() or UTC_TIMESTAMP() to generate date and time. Time is of the essence as you want at least 48 hours between generation and the message. If a player gets a fine the 15th at 23:59 and gets a message the 17th at 00:01 then that's not two days.

More information: http://dev.mysql.com/doc/refman/5.5/..._utc-timestamp
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Use either UNIX_TIMESTAMP() or UTC_TIMESTAMP() to generate date and time. Time is of the essence as you want at least 48 hours between generation and the message. If a player gets a fine the 15th at 23:59 and gets a message the 17th at 00:01 then that's not two days.

More information: http://dev.mysql.com/doc/refman/5.5/..._utc-timestamp
Very true, alright, well I'll change from CURDATE() to UTC_TIMESTAMP(). However this still won't allow me to actually retrieve if it has been two days surely from the SAMP script its self? If, once the 48 hours had passed, how can I get around actually comparing the dates and time to ensure it has been 48 hours? My method would be strcmp of the UTC_TIMESTAMP but it's a rather grey area for me to work out truthfully.
Reply
#4

Are you saving this 48 hours only counting when they're logged in? or both logged in and out. If it's just logged in use timers.
Reply
#5

Quote:
Originally Posted by MD5
Посмотреть сообщение
Are you saving this 48 hours only counting when they're logged in? or both logged in and out. If it's just logged in use timers.
If it was logged in, I'd be capable of doing it with as you suggested, timers. However this time I'm actually wishing to use the system offline too. It's part of an educational thing for me with regards to utilizing similar systems with UNIX_TIMESTAMPS in the future.
Reply
#6

Well, scriptwise there is a gettime(); function, wiki.sa-mp.com/wiki/Gettime
you can simply save that, check if it's been 2*24*60*60 ( 2 days ) since that gettime and save that in MYSQL too (idk if it's possible to save it as date so it would show the actual date in table or not)

So you can simply check onplayerspawn (or whenever you log them in ) to see if it's been more than 2 days for them and show them a msg if you want

On the issueing warrant thingy, you should have a timer that checks gettime - ^^This timestamp saved in var > 2*24*60*60 if yes move it to warrants, trash the variable ( either set it to highest value possible (some day in 2030 ?) or just 0 and exclude 0 from calculations )
Reply
#7

Quote:
Originally Posted by LiamM
Посмотреть сообщение
how can I get around actually comparing the dates and time to ensure it has been 48 hours?
Use TIMEDIFF() function and add some statement in the query.
This function will return time (hour:minuteecond) from two dates.


or you can use DATEDIFF().
Reply
#8

Quote:
Originally Posted by Lczy
Посмотреть сообщение
Use TIMEDIFF() function and add some statement in the query.
This function will return time (hour:minuteecond) from two dates.


or you can use DATEDIFF().
Right, then would I have to use something like Strmid() to actually check if it has 42 or more in it? Seeing as TIMEDIFF() returns hours first, I could simply just store the value into a string, then check the first two characters in the string, convert it to a value (strval) then check if(strval(string) > 42) for example?

Edit: Probably using firstchars() would be better.


Further Edit: I have now resolved the problem, and it is fully working, thanks to Vince for suggesting UTC_TIMESTAMP, and for Lczy suggesting TIMEDIFF. I've now made a fully working system that calculates the payment overdue by days to the player.

PHP код:
//Higher in the script.
format(querysizeof(query), "SELECT TIMEDIFF('%s', NOW())"CharInfo[i][FineDue]);
mysql_function_query(dbHandlequerytrue"WarrantTimeCheck""ii"playeridi);
FP::WarrantTimeCheck(playeridi)
{
    new 
timediff[128],
        
CorrectedTime[64],
        
TimeCheck[64];
    
    
cache_get_row(00timediff);
    
strmid(CorrectedTimetimediff19); // --- This removes the negative symbol to prevent it appearing like -HH:MM:SS
    
new HourIndex strfind(CorrectedTime":"true0); //Locates the first colon symbol splitting the hours up. (In case hours might be three digits as opposed to two, for example, 102 hours and not 59 hours. Making it HHH:MM:SS
    
strmid(TimeCheckCorrectedTime0HourIndex); // Checking if hours exceeds 42 hours as I take the first few characters before the colon symbol (:).
    
new DayCalc strval(TimeCheck) / 24//I break down the hours into days and disregard any potential decimal points afterwards as in theory it isn't actually classed as a day, for example. 5 and a half days, really is only 5 days overdue.
    
    
if(strval(TimeCheck) > 42SendClientMessageF(playeridCOLOR_RED"Warrant Issued{6896F1} - %s (ID: %d) - Citations Due: $%s. Overdue Payment: %d Day(s)"GetPlayerRPName(i), iComma(CharInfo[i][FineTotal]), DayCalc);
    else 
SendClientMessageF(playeridCOLOR_SKYBLUE"%s (ID: %d) - Citations Due: $%s | Citations Issued On: %s"GetPlayerRPName(i), iComma(CharInfo[i][FineTotal]), CharInfo[i][FineDue]);
    
//SendClientMessageF(playerid, COLOR_RED, "No Warrant Issued, 42 hours has not passed. Fined %d day(s) ago.", DayCalc);
//Display the message.
    
return 1;

Thanks to everyone who offered assistance, I just needed a slight kickstart!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)