Age Calculate
Hey,

I would calculate the age of the player, who enter his/her date of birth.

I would make it in a dialog, when a user registers.

I have this so far:

Код:
`#define DIALOG_ALTER 56 //define dialog of age`
Код:
```if(dialogid == DIALOG_ALTER)
{
if(response == 1)
{
new string[35];
new alter = strval(inputtext);
SetPVarInt(playerid,"Alter",alter);
format(string, sizeof(string), "You are %i years old?",alter);
SendClientMessage(playerid,COLOR_WHITE,string);
}

if(response == 0)
{
SendClientMessage(playerid,COLOR_RED,"You are leaving the Server!");
Kick(playerid);
}
return 1;
}```
The easy way: create a function to transform the date into a number:

pawn Код:
`// @UNTESTED// Number of days in the given month, no leap yearsnew daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};stock GetDateIndex(day, month, year){    // Presume 365 days a year    new index = year * 365 + day;    // Add the days of the months that already passed    for (new i = 0; i < month; i++) index += daysInMonth[i];    return index;}`
Do this for the current date, and for the birthdate, subtract them, and divide it by 365 and you got the approximated age.

The hard way: Do the same as above, but add the rules for leap years etc to get the exact number of days.
}
Or you use the getdate function

pawn Код:
`stock BirthAge[MAX_PLAYERS];`
Quote:
 Originally Posted by Nero_3D pawn Код: `stock SetPlayerBirthday(playerid, day, month, year) {    return (BirthAge[playerid] = year * 10000 + month * 100 + day);}`
Quote:
 Originally Posted by Nero_3D pawn Код: `stock GetAge(playerid) {    new        day,        year,        month    ;    getdate(year, month, day);    month = month * 100 + day; // current month and day    day = BirthAge[playerid] % 10000; // extracts month and day    year = year - BirthAge[playerid] / 10000; // gets the difference in years    return (day <= month) ? (year) : (year - 1); // if current date is higher or equal he already had birthday}`

Forum Jump:

Users browsing this thread: 1 Guest(s)