calculate the "months" component of a person's age
Posted: 2019-05-15 05:13
(appgini 5.75 user - TABLE: anagrafica_persone - FIELDS: data_nascita - eta - mesi)
Goodmorning everyone,
I would like to set a field "mesi" (=months) in my table "anagrafica_persone" (=people registry) to automatically calculate the "months" component of a person's age, for example, if the person is 47 and 4 months when the person fills in the "data_nascita" (=date of birth) field by entering 15/01/1972 in the "mesi" field the number 4 should automatically appear.
In the hook file I already have a "eta" (=years) field to calculate, taking the example above, the number 47, but I don't know how to set the formula to get even 11 for the "mesi" (=months) field.
here is how I customized the hook file to get a person's years starting from the "data_nascita" (=date of birth) field:
function anagrafica_persone_init(&$options, $memberInfo, &$args){
/* calculates a person's age */
if(isset($_REQUEST['SelectedID'])){
$id = makeSafe($_REQUEST['SelectedID']);
$today = date('Y-m-d');
sql("update anagrafica_persone set eta=floor(datediff('{$today}', data_nascita) / 365.25) where pkey='{$id}'", $eo);
}
/* here i need to write something for calculates the MONTHLY component of a person's age */
return TRUE;
}
I hope someone more expert than me can help me define the code to reach my goal!
Thank you so much!
Goodmorning everyone,
I would like to set a field "mesi" (=months) in my table "anagrafica_persone" (=people registry) to automatically calculate the "months" component of a person's age, for example, if the person is 47 and 4 months when the person fills in the "data_nascita" (=date of birth) field by entering 15/01/1972 in the "mesi" field the number 4 should automatically appear.
In the hook file I already have a "eta" (=years) field to calculate, taking the example above, the number 47, but I don't know how to set the formula to get even 11 for the "mesi" (=months) field.
here is how I customized the hook file to get a person's years starting from the "data_nascita" (=date of birth) field:
function anagrafica_persone_init(&$options, $memberInfo, &$args){
/* calculates a person's age */
if(isset($_REQUEST['SelectedID'])){
$id = makeSafe($_REQUEST['SelectedID']);
$today = date('Y-m-d');
sql("update anagrafica_persone set eta=floor(datediff('{$today}', data_nascita) / 365.25) where pkey='{$id}'", $eo);
}
/* here i need to write something for calculates the MONTHLY component of a person's age */
return TRUE;
}
I hope someone more expert than me can help me define the code to reach my goal!
Thank you so much!