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(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)); 
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(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!.
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 
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);
} 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)