Like @zwolofmeister already said it.
It's better store and use unix epoch time (seconds).
You can use SQLite as calculator, tough
PHP Code:
public void OnPluginStart()
{
RegConsoleCmd("sm_test", test);
}
public Action test(int client, int args)
{
/*
strftime(): A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
*/
char CurrentDate[50];
FormatTime(CurrentDate, sizeof(CurrentDate), "%Y-%m-%d %H:%M:%S");
//int seconds = GetSecondsBetweenDates("2020-05-01", "2020-06-10");
//int seconds = GetSecondsBetweenDates("2020-05-01 01:22:10", "2020-06-10 20:01:11");
int seconds = GetSecondsBetweenDates("now", "2020-06-13 16:55:11");
PrintToServer("seconds %i", seconds);
PrintToServer("days %i, hours %i, minutes %i, seconds %i", seconds / 86400, (seconds / 3600)%24, (seconds / 60)%60, seconds%60);
return Plugin_Handled;
}
int GetSecondsBetweenDates(const char[] FirstDate, const char[] SecondDate)
{
char error[512];
Database db = SQLite_UseDatabase("sourcemod-local", error, sizeof(error));
if(db == null)
{
LogError("%s", error);
return -1;
}
char query[200];
if(StrEqual(FirstDate, "now", true))
{
Format(query, sizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s','localtime');", "%s", SecondDate, "%s", FirstDate);
}
else
{
Format(query, sizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s');", "%s", SecondDate, "%s", FirstDate);
}
PrintToServer("%s", query)
DBResultSet results = SQL_Query(db, query, sizeof(query));
if(results == null)
{
LogError("DBResultSet results: invalid handle");
delete db;
return -1;
}
if(!results.HasResults || results.RowCount <= 0)
{
delete db;
return -1;
}
int seconds = -1;
if(results.FetchRow())
{
seconds = results.FetchInt(0);
}
delete results;
delete db;
return seconds;
}
I made this quick. Haven't check more deeper in this.
*edit
With SQLite you can also do fancy date converts.
https://www.sqlite.org/lang_datefunc.html
__________________