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

**URL:** <https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842>\
**Category:** Sample Codes\
**Created:** [May 2, 2022, 10:45pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842 "2022-05-02T22:45:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [May 2, 2022, 10:45pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/1 "2022-05-02T22:45:31Z")

</div>

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**

 ![image](https://forum.radsystems.io/uploads/default/original/1X/fbf322f95a67bbe30436fcbc68fab334e181f532.png)

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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/e2ab8e515956c0ac8f4e34b25a157086d865c393.png)

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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/2441e5497d505ffdbff26199f0645885f9025c74.png)

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

you should now have this when you publish your app

 ![image](https://forum.radsystems.io/uploads/default/original/1X/4fd0bb9ec4d418db3a6979617611142dbec12717.png)  
! good now finally we finish the app!  
go to \*\*page events \>\> before add of your table \*\*  
 ![image](https://forum.radsystems.io/uploads/default/original/1X/e8a7262d62c9c5f7d1265b20d76458d293ca62ba.png)

and write the following code

```auto
$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

 ![image](https://forum.radsystems.io/uploads/default/original/1X/4029f2589dfa0c5942873c6d27075b461cba51e3.png)

! that will be all! happy coding!

---

<div class="post-metadata">

**Author:** ![thamrin](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/t/6a8cbe/32.png) [@thamrin](https://forum.radsystems.io/u/thamrin)\
**Post date:** [June 22, 2022, 9:14am UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/2 "2022-06-22T09:14:09Z")

</div>

thanks sir  
helping me

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [June 22, 2022, 9:27am UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/3 "2022-06-22T09:27:53Z")

</div>

@thamrin welcome glad to have helped…

---

<div class="post-metadata">

**Author:** ![yayi](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yayi](https://forum.radsystems.io/u/yayi)\
**Post date:** [June 28, 2024, 2:32pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/4 "2024-06-28T14:32:58Z")

</div>

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

 ![01](https://forum.radsystems.io/uploads/default/original/2X/c/c050d3f72308ac9da676a5fbe9579a2369547fd9.png)

---

<div class="post-metadata">

**Author:** ![yayi](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yayi](https://forum.radsystems.io/u/yayi)\
**Post date:** [June 28, 2024, 2:33pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/5 "2024-06-28T14:33:53Z")

</div>

![02](https://forum.radsystems.io/uploads/default/original/2X/a/ad0c8e2a18e020b2fea61e971209e44a07573b18.png)

---

<div class="post-metadata">

**Author:** ![yayi](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yayi](https://forum.radsystems.io/u/yayi)\
**Post date:** [June 28, 2024, 2:35pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/6 "2024-06-28T14:35:01Z")

</div>

![03](https://forum.radsystems.io/uploads/default/original/2X/a/ad2ea36b415bcd2f69dd4f4aac9b71fab4ad9e4b.png)

---

<div class="post-metadata">

**Author:** ![Wahome-mutahi100](https://forum.radsystems.io/user_avatar/forum.radsystems.io/wahome-mutahi100/32/16_2.png) [@Wahome-mutahi100](https://forum.radsystems.io/u/Wahome-mutahi100)\
**Post date:** [July 4, 2024, 2:44pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/7 "2024-07-04T14:44:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcopolo1](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/m/8edcca/32.png) [@marcopolo1](https://forum.radsystems.io/u/marcopolo1)\
**Post date:** [July 14, 2024, 7:29pm UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/8 "2024-07-14T19:29:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ddiaz](https://forum.radsystems.io/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@ddiaz](https://forum.radsystems.io/u/ddiaz)\
**Post date:** [February 1, 2025, 3:33am UTC](https://forum.radsystems.io/t/how-to-calculate-age-based-on-date-of-birth-in-phprad-classic-2-7-3/842/9 "2025-02-01T03:33:40Z")

</div>

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](https://drive.google.com/file/d/137ULdqYNLF2q8_ohBmCUMCFO7NzVTNRT/view?usp=drive_link)

 ![edad](https://forum.radsystems.io/uploads/default/original/2X/3/33b95473b287217030f0366f72feb20b6966d37a.jpeg)

```auto
$('#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);
});

```
