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(year, month, day, byYear, byMonth, byDay) {
new realYear = year-byYear*365;
new monthAmount;
switch(month) {
case 1: monthAmount = 31;
case 2: monthAmount = 28;
case 3: monthAmount = 31;
case 4: monthAmount = 30;
case 5: monthAmount = 31;
case 6: monthAmount = 30;
case 7: monthAmount = 31;
case 8: monthAmount = 31;
case 9: monthAmount = 30;
case 10: monthAmount = 31;
case 11: monthAmount = 30;
case 12: monthAmount = 31;
}
new monthAmount2;
switch(byMonth) {
case 1: monthAmount2 = 31;
case 2: monthAmount2 = 28;
case 3: monthAmount2 = 31;
case 4: monthAmount2 = 30;
case 5: monthAmount2 = 31;
case 6: monthAmount2 = 30;
case 7: monthAmount2 = 31;
case 8: monthAmount2 = 31;
case 9: monthAmount2 = 30;
case 10: monthAmount2 = 31;
case 11: monthAmount2 = 30;
case 12: monthAmount2 = 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));