calculate the "months" component of a person's age

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 70
Joined: 2019-04-30 17:37

calculate the "months" component of a person's age

Post by fgazza » 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!

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 525
Joined: 2018-04-01 10:12

Re: calculate the "months" component of a person's age

Post by pböttcher » 2019-05-15 06:49

Hi,

you may try it like this:

Code: Select all

if(isset($_REQUEST['SelectedID'])){
$id = makeSafe($_REQUEST['SelectedID']);
date_default_timezone_set('UTC');
$birth=sqlvalue("SELECT data_nascita from anagrafica_persone where pkey='{$id}'");
$date1 = new DateTime();
$date2 = new DateTime($birth);
$interval = $date1->diff($date2);
$years=$interval->format('%y');
$month=$interval->format('%m');

sql("update anagrafica_persone set eta='{$years}', mesi='{$month}' where pkey='{$id}'", $eo);

}

fgazza
Veteran Member
Posts: 70
Joined: 2019-04-30 17:37

Re: calculate the "months" component of a person's age

Post by fgazza » 2019-05-16 13:02

Hello. Thank you! It works perfectly.
I am now trying to make the fields "eta" (=years) and mesi (=months) so that they are read-only and update themselves during the compilation of the form.
When I only had the "eta" field I had set up an anagrafica_persone_dv.js file with this code:

$j(function(){
$j('#eta').prop('readonly', true);

$j('#data_nascita-dd, #data_nascita-mm, #data_nascita').change(function(){
var dob = get_date('data_nascita');
var today = new Date();
var age = Math.floor((today - dob) / 1000 / 60 / 60 / 24 / 365.25);

$j('#eta').val(age);
});

$j('#data_nascita').change();
});

can you tell me what code to insert to have both the "eta" field (= years) and the "months" field (= months) as read-only fields that are completed when filling in the form?
Thank you!
Fabiano

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 525
Joined: 2018-04-01 10:12

Re: calculate the "months" component of a person's age

Post by pböttcher » 2019-05-17 11:20

Hi,

you may try

Code: Select all

var dob = get_date('data_nascita');
var today = new Date();
var age = (new Date(today-dob)).getFullYear()-1970;
var month=(new Date(today-dob)).getMonth();

Post Reply