How to obtain age from date of birth? -
Jose_grana - 11.08.2018
In my gm I save the date in a string with the format Day / Month / Year.
Example:
02/21/1990
How could I get the age by getting the current server date?
Example.
21/02/1990.- 12/08/2018 = 28 years.
+1 reputation
Re: How to obtain age from date of birth? -
Jefff - 12.08.2018
pawn Код:
new Age = strval(date_now[6]) - strval(birth_date[6]);
Re: How to obtain age from date of birth? -
Gammix - 12.08.2018
PHP код:
numdays(year, month) {
switch (month) {
case 4, 6, 9, 11: {
return 30;
}
case 2: {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
}
return 28;
}
}
return 31;
}
getage(birth_year, birth_month, birth_day) {
new current_year, current_month, current_day;
getdate(current_year, current_month, current_day);
new sum;
new cumulative_days[12];
for (new i = 0; i < 12; ++i){
sum += numdays(current_year, i);
cumulative_days[i] = sum;
}
new age = (current_year - birth_year - 1);
new days_this_year = (current_day + cumulative_days[current_month - 1]);
new days_birth_year = (cumulative_days[11] - (birth_day + cumulative_days[birth_month - 1]));
new total_days = (days_this_year + days_birth_year);
age += total_days/365;
return age;
}
// how to use
printf("Age: %i", getage(1995, 6, 30));
Re: How to obtain age from date of birth? -
Jefff - 12.08.2018
Why not
pawn Код:
new Age = current_year - birth_year;
? xd
Re: How to obtain age from date of birth? -
Private200 - 12.08.2018
Quote:
Originally Posted by Jefff
Why not
pawn Код:
new Age = current_year - birth_year;
? xd
|
cause it will be inaccurate once it comes to specific days (everyone from 1998 would be the same age in 201
.
Re: How to obtain age from date of birth? -
CaptainBoi - 12.08.2018
you can do it by taking the birth year and taking todays year
and make a format in which its going to take subtracted
then send client message
this is your age
Re: How to obtain age from date of birth? -
Jose_grana - 12.08.2018
Quote:
Originally Posted by Gammix
PHP код:
numdays(year, month) {
switch (month) {
case 4, 6, 9, 11: {
return 30;
}
case 2: {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
}
return 28;
}
}
return 31;
}
getage(birth_year, birth_month, birth_day) {
new current_year, current_month, current_day;
getdate(current_year, current_month, current_day);
new sum;
new cumulative_days[12];
for (new i = 0; i < 12; ++i){
sum += numdays(current_year, i);
cumulative_days[i] = sum;
}
new age = (current_year - birth_year - 1);
new days_this_year = (current_day + cumulative_days[current_month - 1]);
new days_birth_year = (cumulative_days[11] - (birth_day + cumulative_days[birth_month - 1]));
new total_days = (days_this_year + days_birth_year);
age += total_days/365;
return age;
}
// how to use
printf("Age: %i", getage(1995, 6, 30));
|
Your code works perfect but only when defining the day, month and year.
My code:
pawn Код:
enum pInfo
{
pBirthDate[24]
};
new PlayerInfo[MAX_PLAYERS][pInfo];
//Load variable
pawn Код:
cache_get_value_name(0, "Birthdate", PlayerInfo[extraid][pBirthDate],24);
//To modify the variable
pawn Код:
format(PlayerInfo[playerid][pBirthDate], 24, inputtext);
How can I use your function like this?
Thanks for you time!.
Re: How to obtain age from date of birth? -
Gammix - 13.08.2018
You'll have to split your string.
If your birthdate has this type of format: "21/02/1990"
You can do something like this:
PHP код:
test_function() {
new string[] = "21/02/1990";
new year, month, day;
new pos, tmp[5];
strmid(tmp, string, 0, (pos = strfind(string, "/", true, 0)));
day = strval(tmp);
strmid(tmp, string, pos + 1, (pos = strfind(string, "/", true, pos + 1)));
month = strval(tmp);
strmid(tmp, string, pos + 1, strlen(string));
year = strval(tmp);
printf("%i/%i/%i", day, month, year);
}