Raised This Month: $ Target: $400
 0% 

[SOLVED] Add X days in a date


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-07-2016 , 13:09   [SOLVED] Add X days in a date
Reply With Quote #1

I'm trying to add days in a specific date. So, let's say we have the date in a string format: "07.05.2016", and I want to add 45 days, so it would become "21.06.2016" (again a string). Is there any efficient way of doing this?

Last edited by OciXCrom; 06-27-2016 at 17:48.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-07-2016 , 13:41   Re: Add X days in a date
Reply With Quote #2

You can do this using the Unix Time include.

With this you can add or subtract days to/from a date using a positive or negative number as iDaysToAdd, respectively. I included functions to do this to a date string or to a date value in integer format. The date string must be in 'DD.MM.YYYY' format for this to work properly.

Output:
Code:
New date=12.05.2016
PHP Code:
#include <amxmodx>
#include <unixtime>

new const Version[] = "0.1";

public 
plugin_init() 
{
    
register_plugin"Add Days to Date" Version "bugsy" );
    
    
register_concmd"test" "Test" );
}

public 
Test()
{
    new 
szDate[] = "07.05.2016";

    
AddDays_StringszDate charsmaxszDate ) , );
    
    
server_print"New date=%s" szDate );
}

public 
AddDays_StringszDate[] , iMaxChars iDaysToAdd )
{
    new 
iDay iMonth iYear;
    
    
szDate] = EOS;
    
szDate] = EOS;    
    
    
iDay str_to_numszDate] );
    
iMonth str_to_numszDate] );
    
iYear str_to_numszDate] );
    
    
AddDaysiDay iMonth iYear iDaysToAdd );
    
    return 
formatexszDate iMaxChars "%02d.%02d.%04d" iDay iMonth iYear );
}

public 
AddDays( &iDay , &iMonth , &iYear iDaysToAdd )
{
    new 
iNewDate iHours iMinutes iSeconds;
    
    
iNewDate TimeToUnixiYear iMonth iDay );
    
    
iNewDate += ( iDaysToAdd 86400 );
    
    
UnixToTimeiNewDate iYear iMonth iDay iHours iMinutes iSeconds );

__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-07-2016 , 13:42   Re: Add X days in a date
Reply With Quote #3

First convert it to a timestamp, add the appropriate number of seconds and then reconvert it it a date.
__________________
fysiks is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-07-2016 , 14:43   Re: Add X days in a date
Reply With Quote #4

Thank you bugsy, that worked perfectly.
Thank you too, fysiks.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-07-2016 , 15:50   Re: Add X days in a date
Reply With Quote #5

Bugsy, AMXX has natives for dealing with that, you don't need custom include.

PHP Code:
AddToDate(const OriginalDate[], const DaysToAddFinalDate[], const Size)
{
    new const 
FormatRule[] = "%d.%m.%Y"
    
new const SecondsInDay 86400
    
    
new CurrentTimeStamp parse_time(OriginalDateFormatRule)
    
CurrentTimeStamp CurrentTimeStamp DaysToAdd SecondsInDay
    format_time
(FinalDateSizeFormatRuleCurrentTimeStamp)

You can use something like this, should be better. d is day, m is month, Y is year. You can also change the format of date, use / instead of . or make it m/d/y instead of d/m/y.

An example:
PHP Code:
#include <amxmodx>

public plugin_init()
{
    
register_srvcmd("output_date""ServerCommand_OutputDate")
}

public 
ServerCommand_OutputDate()
{
    new 
CurrentDate[11], FinalDate[11]
    
get_time("%d.%m.%Y"CurrentDatecharsmax(CurrentDate))
    
AddToDate(CurrentDate5FinalDatecharsmax(FinalDate))
    
    
server_print("CurrentDate: %s | NewDate: %s"CurrentDateFinalDate)
}

AddToDate(const OriginalDate[], const DaysToAddFinalDate[], const Size)
{
    new const 
FormatRule[] = "%d.%m.%Y"
    
new const SecondsInDay 86400
    
    
new CurrentTimeStamp parse_time(OriginalDateFormatRule)
    
CurrentTimeStamp CurrentTimeStamp DaysToAdd SecondsInDay
    format_time
(FinalDateSizeFormatRuleCurrentTimeStamp)

__________________

Last edited by HamletEagle; 05-07-2016 at 16:58.
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-07-2016 , 15:53   Re: Add X days in a date
Reply With Quote #6

I thought there might be built-in stuff, but couldn't think of the function name.
__________________
Bugsy is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 05-08-2016 , 08:22   Re: Add X days in a date
Reply With Quote #7

Thank you, HamletEagle. That does look better and works just as well. I will stick to your method, since it doesn't require any additional includes.

Last edited by OciXCrom; 05-08-2016 at 08:22.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
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 18:38.


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