AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes (https://forums.alliedmods.net/showthread.php?t=312425)

Croxeye. 12-02-2018 17:36

DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
I need function that returns difference between two datetimes in seconds.

Bugsy 12-02-2018 17:45

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
I recommend storing time in unix timestamp format, much easier to manage and do calculations with.

Croxeye. 12-02-2018 18:21

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
Thank you, I didn't know about that before.

EDIT: can you just give me an example of the best way to get difference ?

E1_531G 12-02-2018 18:42

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
new diff = get_systime() - timestamp

Bugsy 12-02-2018 19:11

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
Yes, and you can easily convert a timestamp to any time value you want, down to the second.

Croxeye. 12-03-2018 17:31

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
So I have to use TimeToUnix to convert DATETIME from database to unix format ?

Natsheh 12-03-2018 17:43

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
How are you saving the date in the database as Unix timestamp value or as string date value.?


If you are saving it as a date you need to convert it to unix timestamp using bugsy include file for converting the date to unix its for easier purpose https://forums.alliedmods.net/showth...=91915?t=91915

Croxeye. 12-03-2018 18:03

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
Code:

warning 213: tag mismatch
PHP Code:

    new iTimeDifiTimeiRefDateiYeariMonthiDayiHouriMinuteiSecond;
    new 
refDate "2018-12-02 12:03:45"
    
    
iTime get_systime();
    
iRefDate TimeToUnix(refDateiYeariMonthiDayiHouriMinuteiSecond); //error
    
    
iTimeDif iTime iRefDate;
    
    
UnixToTime(iTimeDifiYeariMonthiDayiHouriMinuteiSecond);
    
    
console_print(0"iTimeDif: %s"iTimeDif); 


Bugsy 12-03-2018 18:49

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
You cannot use the string version of time with this method.

PHP Code:

public CalcTimeDiff()
{
    new 
iTimeDif iTime iRefDate iYear iMonth iDay iHour iMinute iSecond;

    
//Not sure why, but get_systime() now needs a -18000 offset, this may vary based on where your server is located.
    
iTime get_systime( -18000 );

    
iRefDate TimeToUnix2018 12 18 40 );
    
    
iTimeDif iTime iRefDate;
    
    if ( 
iTimeDif >= 31536000 )
    {
        
iYear iTimeDif 31536000;
        
iTimeDif %= 31536000;
    }
    
    if ( 
iTimeDif >= 2678400 )
    {
        
iMonth iTimeDif 2678400;
        
iTimeDif %= 2678400;
    }
    
    if ( 
iTimeDif >= 86400 )
    {
        
iDay iTimeDif 86400;
        
iTimeDif %= 86400;
    }
    
    if ( 
iTimeDif >= 3600 )
    {
        
iHour iTimeDif 3600;
        
iTimeDif %= 3600;
    }
    
    if ( 
iTimeDif >= 60 )
    {
        
iMinute iTimeDif 60;
        
iTimeDif %= 60;    
    }
    
    if ( 
iTimeDif )
    {
        
iSecond iTimeDif;
    }    
    
    
server_print"%d years, %d months, %d days, %d hours, %d minutes, %d seconds" iYear iMonth iDay iHour iMinute iSecond );



shauli 12-04-2018 08:59

Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
 
Quote:

Originally Posted by Croxeye. (Post 2626636)
Code:

warning 213: tag mismatch
PHP Code:

    new iTimeDifiTimeiRefDateiYeariMonthiDayiHouriMinuteiSecond;
    new 
refDate "2018-12-02 12:03:45"
    
    
iTime get_systime();
    
iRefDate TimeToUnix(refDateiYeariMonthiDayiHouriMinuteiSecond); //error
    
    
iTimeDif iTime iRefDate;
    
    
UnixToTime(iTimeDifiYeariMonthiDayiHouriMinuteiSecond);
    
    
console_print(0"iTimeDif: %s"iTimeDif); 


parse_time will be perfect here:
PHP Code:

    new iTimeiTimeDif;
    new 
refDate[ ] = "2018-12-02 12:03:45";
    
    
iTime parse_time(refDate"%Y-%m-%d %H:%M:%S");
    
iTimeDif get_systime() - iTime;

    
console_print(0"iTimeDif: %d"iTimeDif); 

This code assumes that your refDate is in the past, if it's in the future just play with iTimeDif.
It will print the time difference in seconds, if you want to print full information (days/months/etc) then take a look at Bugsy's code right above me.


All times are GMT -4. The time now is 07:36.

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