How to obtain age from date of birth?
#1

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
Reply
#2

pawn Код:
new Age = strval(date_now[6]) - strval(birth_date[6]);
Reply
#3

PHP код:
numdays(yearmonth) {
    switch (
month) {
        case 
46911: {
            return 
30;
          }
        case 
2: {
            if ((
year == && year 100 != 0) || (year 400 == 0)) {
                return 
29;
            }
            return 
28;
          }
    }
    
    return 
31;
}
getage(birth_yearbirth_monthbirth_day) {
    new 
current_yearcurrent_monthcurrent_day;
    
getdate(current_yearcurrent_monthcurrent_day);
    new 
sum;
    new 
cumulative_days[12];
    for (new 
012; ++i){
        
sum += numdays(current_yeari);
        
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(1995630)); 
Reply
#4

Why not

pawn Код:
new Age = current_year - birth_year;
? xd
Reply
#5

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.
Reply
#6

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
Reply
#7

Quote:
Originally Posted by Gammix
Посмотреть сообщение
PHP код:
numdays(yearmonth) {
    switch (
month) {
        case 
46911: {
            return 
30;
          }
        case 
2: {
            if ((
year == && year 100 != 0) || (year 400 == 0)) {
                return 
29;
            }
            return 
28;
          }
    }
    
    return 
31;
}
getage(birth_yearbirth_monthbirth_day) {
    new 
current_yearcurrent_monthcurrent_day;
    
getdate(current_yearcurrent_monthcurrent_day);
    new 
sum;
    new 
cumulative_days[12];
    for (new 
012; ++i){
        
sum += numdays(current_yeari);
        
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(1995630)); 
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!.
Reply
#8

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 
yearmonthday;
    new 
postmp[5];

    
strmid(tmpstring0, (pos strfind(string"/"true0)));
    
day strval(tmp);
    
    
strmid(tmpstringpos 1, (pos strfind(string"/"truepos 1)));
    
month strval(tmp);
    
    
strmid(tmpstringpos 1strlen(string));
    
year strval(tmp);

    
printf("%i/%i/%i"daymonthyear);

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)