Hey everyone, I’m having trouble calculating age from a birthdate on the server side. I’ve tried using row_inserting and row_updating functions, but the age doesn’t auto-update as the year changes.
How can I calculate age based on a birthdate (d/m/y) so that it updates automatically each year? Could someone share an example code and explain how to implement it? Thanks!
Where should I place coding?
php code
$birth_date = '12'-04-1999;
$current_date = date('d-m-Y');
$birth_date_obj = new DateTime($birth_date);
$current_date_obj = new DateTime($current_date);
$diff = $current_date_obj->diff($birth_date_obj);
$age_in_years = $diff->y;
echo "The Age Is.......:.". $age_in_years ."years old";
if you want to calculate the ager when you are inserting the record, then put the code in Row_Inserting server event, this is fired before the commit of Row_Inserted.if you want to display it on your list page then do the calc in Row_Rendered, e.g.$this->employeeAge->ViewValue = "Age: ". $age_in_years