AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] Add X days in a date (https://forums.alliedmods.net/showthread.php?t=282456)

OciXCrom 05-07-2016 13:09

[SOLVED] Add X days in a date
 
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?

Bugsy 05-07-2016 13:41

Re: Add X days in a date
 
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 );



fysiks 05-07-2016 13:42

Re: Add X days in a date
 
First convert it to a timestamp, add the appropriate number of seconds and then reconvert it it a date.

OciXCrom 05-07-2016 14:43

Re: Add X days in a date
 
Thank you bugsy, that worked perfectly.
Thank you too, fysiks.

HamletEagle 05-07-2016 15:50

Re: Add X days in a date
 
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)



Bugsy 05-07-2016 15:53

Re: Add X days in a date
 
I thought there might be built-in stuff, but couldn't think of the function name.

OciXCrom 05-08-2016 08:22

Re: Add X days in a date
 
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.


All times are GMT -4. The time now is 18:38.

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