How to calculate age based on date of birth in phprad classic 2.7.3++

well hello there ! let us calculate the age based on date of birth!!
let us start by creating our table!
i created one table with the following fields! dob and age

that is it for the database! next thing is to now hide the age field and set it as not required as this will be updated in the background! we only want the date of birth field so that the user can enter when they were born!
click on the add page

then click on age >> TextBoxType and set it as hidden
then click on required and set it to false! like this!

this will just make sure rad doesnt throw errors that age field is required!

you should now have this when you publish your app


! good now finally we finish the app!
go to **page events >> before add of your table **

and write the following code

$age = date_diff(date_create($modeldata['dob']), date_create(date_now()));

$modeldata['age'] = $age->format("%y");

now when you enter your dates you should have this

! that will be all! happy coding!

1 Like

thanks sir
helping me

1 Like

@thamrin welcome glad to have helped…

Hello team I have the same problem I have radsystem studio =8.7.0 I can’t display the age

there is a function you removed here that comes with rad by default

Hello, I am trying to apply your code but age is always returning Null value, am I missing something?

my pageevent code:

private function beforeAdd($modeldata){ $age = date_diff(date_create($modeldata[‘birthdate’]), date_create(date_now())); $modeldata[‘age’] = $age->format(“%y”); }

If you are using Laravel Bootstrap, the simplest way is to do it using jQuery through clientevents in the date of birth field.

Video: Calculate age

$('#ctrl-fechanacimiento').on('change', function() {
    // Obtener el valor de la fecha de nacimiento
    var fechaNac = new Date($(this).val());
    var fechaActual = new Date();
    
    // Calcular la edad
    var edad = fechaActual.getFullYear() - fechaNac.getFullYear();
    
    // Ajustar la edad si aún no ha llegado el cumpleaños este año
    if (fechaNac.getMonth() > fechaActual.getMonth() || 
        (fechaNac.getMonth() === fechaActual.getMonth() && 
         fechaNac.getDate() > fechaActual.getDate())) {
        edad--;
    }
    
    // Actualizar el campo de edad
    $('#ctrl-edad').val(edad);
});