SA-MP Forums Archive
How to obtain age from date of birth? - 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 to obtain age from date of birth? (/showthread.php?tid=657617)



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(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)); 



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(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!.


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 
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);