Raised This Month: $51 Target: $400
 12% 

Check days between date


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 05-13-2020 , 07:10   Check days between date
Reply With Quote #1

PHP Code:
    char CurrentDate[20];
    
FormatTime(CurrentDatesizeof(CurrentDate), "%Y%m%d");
    
PrintToChatAll(CurrentDate);
    
char date1[64] = "20200601";
    
char date2[64] = "20200531";
    
PrintToChatAll("%d"StringToInt(date1) - StringToInt(date2)); 
with the above i didn't get 1

but if change date1 to 20200531 and date2 to 20200530 i get 1 which is correct

how do i get the correct day?
__________________
8guawong is offline
zwolofmeister
Member
Join Date: Aug 2018
Old 05-13-2020 , 08:57   Re: Check days between date
Reply With Quote #2

So basically you can do this;
int iNow = GetTime();
int iTomorrow = GetTime()+86400;

char szTime[20];
FormatTime(szTime, sizeof(szTime), "%m / %d / %y", iTomorrow); // This is tomorrows date
FormatTime(szTime, sizeof(szTime), "%m / %d / %y", iNow); // This is todays date

if this is not what you mean, you can check out this thread:
Liink
zwolofmeister is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-13-2020 , 09:50   Re: Check days between date
Reply With Quote #3

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 clientint 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(CurrentDatesizeof(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)%60seconds%60);

    return 
Plugin_Handled;
}


int GetSecondsBetweenDates(const char[] FirstDate, const char[] SecondDate)
{
    
char error[512];
    
Database db SQLite_UseDatabase("sourcemod-local"errorsizeof(error));

    if(
db == null)
    {
        
LogError("%s"error);
        return -
1;
    }

    
char query[200];

    if(
StrEqual(FirstDate"now"true))
    {
        
Format(querysizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s','localtime');""%s"SecondDate"%s"FirstDate);
    }
    else
    {
        
Format(querysizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s');""%s"SecondDate"%s"FirstDate);
    }
    
PrintToServer("%s"query)
    
DBResultSet results SQL_Query(dbquerysizeof(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
__________________
Do not Private Message @me

Last edited by Bacardi; 05-13-2020 at 09:52.
Bacardi is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 05-13-2020 , 13:11   Re: Check days between date
Reply With Quote #4

Quote:
Originally Posted by zwolofmeister View Post
So basically you can do this;
int iNow = GetTime();
int iTomorrow = GetTime()+86400;

char szTime[20];
FormatTime(szTime, sizeof(szTime), "%m / %d / %y", iTomorrow); // This is tomorrows date
FormatTime(szTime, sizeof(szTime), "%m / %d / %y", iNow); // This is todays date

if this is not what you mean, you can check out this thread:
Liink
yea i know how to get the date but i want to compare date
too bad you can't compare date the way i wanted when they are different months

Quote:
Originally Posted by Bacardi View Post
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 clientint 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(CurrentDatesizeof(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)%60seconds%60);

    return 
Plugin_Handled;
}


int GetSecondsBetweenDates(const char[] FirstDate, const char[] SecondDate)
{
    
char error[512];
    
Database db SQLite_UseDatabase("sourcemod-local"errorsizeof(error));

    if(
db == null)
    {
        
LogError("%s"error);
        return -
1;
    }

    
char query[200];

    if(
StrEqual(FirstDate"now"true))
    {
        
Format(querysizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s','localtime');""%s"SecondDate"%s"FirstDate);
    }
    else
    {
        
Format(querysizeof(query), "SELECT strftime('%s','%s') - strftime('%s','%s');""%s"SecondDate"%s"FirstDate);
    }
    
PrintToServer("%s"query)
    
DBResultSet results SQL_Query(dbquerysizeof(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
thx i went with the mysql route using DATEDIFF
__________________

Last edited by 8guawong; 05-13-2020 at 13:12.
8guawong is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:15.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode