How to Calculate Age from Date of Birth in PHP
How to Calculate Age from Date of Birth in PHP
Some web applications are needed to show the age of the user. In that case, you need to calculate the user age from date of birth. Here we’ll provide a short PHP code snippet to calculate age from date of birth.
In the following code, date()
, date_create()
, and date_diff()
functions are used to calculate age of the user till today in PHP.
$dateOfBirth = "17-10-1985"; $today = date("Y-m-d"); $diff = date_diff(date_create($dateOfBirth), date_create($today)); echo 'Age is '.$diff->format('%y');
Use the above code to get the age from date of birth using PHP date functions.