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

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Croxeye.
Member
Join Date: Nov 2018
Old 12-02-2018 , 17:36   DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #1

I need function that returns difference between two datetimes in seconds.
Croxeye. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-02-2018 , 17:45   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #2

I recommend storing time in unix timestamp format, much easier to manage and do calculations with.
__________________
Bugsy is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-02-2018 , 18:21   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #3

Thank you, I didn't know about that before.

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

Last edited by Croxeye.; 12-02-2018 at 18:35.
Croxeye. is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 12-02-2018 , 18:42   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #4

new diff = get_systime() - timestamp
__________________
My English is A0
E1_531G is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-02-2018 , 19:11   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #5

Yes, and you can easily convert a timestamp to any time value you want, down to the second.
__________________
Bugsy is offline
Croxeye.
Member
Join Date: Nov 2018
Old 12-03-2018 , 17:31   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #6

So I have to use TimeToUnix to convert DATETIME from database to unix format ?
Croxeye. is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-03-2018 , 17:43   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #7

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
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 12-03-2018 at 17:47.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Croxeye.
Member
Join Date: Nov 2018
Old 12-03-2018 , 18:03   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #8

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); 
Croxeye. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-03-2018 , 18:49   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #9

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 );

__________________
Bugsy is offline
shauli
Member
Join Date: Jun 2018
Old 12-04-2018 , 08:59   Re: DATETIME (yyyy-mm-dd hh:mm:ss) Difference in minutes
Reply With Quote #10

Quote:
Originally Posted by Croxeye. View Post
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.

Last edited by shauli; 12-04-2018 at 09:01.
shauli 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 06:51.


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