SA-MP Forums Archive
How could I be calculating the days between 2 dates? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How could I be calculating the days between 2 dates? (/showthread.php?tid=613643)



How could I be calculating the days between 2 dates? - danielpalade - 30.07.2016

Hello.
How can I calculate the days between two dates like 20.05.2015 and 12.08.2017?
I've searched everywhere, but I can't manage to find anything. Thanks.


Re: How could I be calculating the days between 2 dates? - Luicy. - 30.07.2016

2015-2017 = 2
2x365 = 730

5-8 = 3
3x30 = 90

20-12 = 8

730+90+8 = 828

This isn't really correct due to the extra day each 4 years,also the 30days a month.

In Pawn;
PHP код:
stock GetDateBetween(yearmonthdaybyYearbyMonthbyDay) {
     new 
realYear year-byYear*365;
     new 
monthAmount;
     switch(
month) {
         case 
1monthAmount 31;
         case 
2monthAmount 28;
         case 
3monthAmount 31;
         case 
4monthAmount 30;
         case 
5monthAmount 31;
         case 
6monthAmount 30;
         case 
7monthAmount 31;
         case 
8monthAmount 31;
         case 
9monthAmount 30;
         case 
10monthAmount 31;
         case 
11monthAmount 30;
         case 
12monthAmount 31;
     }
     new 
monthAmount2;
     switch(
byMonth) {
         case 
1monthAmount2 31;
         case 
2monthAmount2 28;
         case 
3monthAmount2 31;
         case 
4monthAmount2 30;
         case 
5monthAmount2 31;
         case 
6monthAmount2 30;
         case 
7monthAmount2 31;
         case 
8monthAmount2 31;
         case 
9monthAmount2 30;
         case 
10monthAmount2 31;
         case 
11monthAmount2 30;
         case 
12monthAmount2 31;
     }
     new 
realMonth monthAmount+monthAmount2;
     new 
realDay day-byDay;
     return 
realYear+realMonth+realDay;




Re: How could I be calculating the days between 2 dates? - Gammix - 30.07.2016

Here is the magic code: (it take cares of leap years as well)

pawn Код:
rdn(year, month, day) // Rata Die day
{
    if (month < 3)
    {
        year--;
        month += 12;
    }
    return ((365*year) + (year/4) - (year/100) + (year/400) + (((153*month) - 457)/5) + (day - 306));
}
How to use:
pawn Код:
new num_days = (rdn(2017, 1, 1) - rdn(2016, 1, 1));