calculate age from date of birth – How to Calculate Age from Date of Birth in PHP?

calculate age from date of birth and display it. The PHP date function is used to format a date. here we can learn to main 2 Examples.

calculate age from date of birth in PHP

Calculate Age to $Date of Birth can be done in many ways. The following source code will calculates age from date of birth.

Example

how_to_calculate_age_from_date_of_birth_in_php.php

<?php
  
    $dob='1994-07-01';
    $year = (date('Y') - date('Y',strtotime($dob)));
  
    echo $year;
  
?>

Result

27

don’t miss : Get Previous Date From Given Date In Php

Also Read This πŸ‘‰   sql left outer join

Example 2:

how_to_calculate_age_from_date_of_birth_in_php_2.php

<?php
  
    $dob = new DateTime('1994-07-01');
    $today   = new DateTime('today');
  
    $year = $dob->diff($today)->y;
   
    echo $year;
  
?>

Result

27

I hope you get an idea about calculate age from date of birth.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.