php

How to Calculate Age from Date of Birth in PHP

In this tutorial, we are going to see how to calculate age from Date of Birth in PHP. Some web applications need to display the user’s age. In this case, you need to calculate the user’s age from the date of birth. We are going to see a PHP code snippet to calculate the age from the date of birth.

In the following code, date(), date_create() and date_diff() functions are used to calculate the user’s age from Date of Birth in PHP.
 

How to Calculate Age from Date of Birth in PHP
<?php
	$dateOfBirth = "15-06-1995";
	$today = date("Y-m-d");
	$diff = date_diff(date_create($dateOfBirth), date_create($today));
	echo 'Your age is '.$diff->format('%y');
?>

Output:

Your age is 26
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *