Age calculator in PHP
In this post, you will learn how to calculate the present age in years, months, and days using the PHP programming language. Sometimes we need to calculate the age of the user in web applications. Calculating age is a very simple process. We will have to find the difference between the current year and the birth year. PHP provides various approaches to calculating the age. Here, we have mentioned most of them.
PHP calculate age using DateTime() method
In the given example, we are calculating the age using the PHP diff() function of DateTime. The current age is found by getting the difference between two dates.
<?php
// PHP program to calculate age in years
// Define the date of birth
$dateOfBirth = '15-03-1987';
// Create a datetime object using date of birth
$dob = new DateTime($dateOfBirth);
// Get current date
$now = new DateTime();
// Calculate the time difference between the two dates
$diff = $now->diff($dob);
// Get the age in years, months and days
echo "Your current age is ".$diff->y." years.
";
// Get the age in years, months and days
echo "Your current age is ".$diff->y." years ".$diff->m." months ".$diff->d." days.";
?>
Sample output of the above program:
Your current age is 34 years.
Your current age is 34 years 5 months 0 days.
PHP calculate age using the date_diff() method
Here, we have used the date_diff() function to calculate the current age in years, months, and days.
<?php
// PHP program to calculate age in years
// Define the date of birth
$dateOfBirth = '20-04-1988';
// Get today's date
$now = date("Y-m-d");
// Calculate the time difference between the two dates
$diff = date_diff(date_create($dateOfBirth), date_create($now));
// Get the age in years, months and days
echo "your current age is ".$diff->format('%y')." Years ".$diff->format('%m')." months ".$diff->format('%d')." days";
?>
Sample output of the above program:
Your current age is 32 Years 4 months 21 days.
PHP calculate age using the subtract method
Here is another simple process to get the current age. To calculate the age, we can simply subtract the birth year from the current year. To implement this, we need to check if the current month and date are less than the birth month and date. If it is, subtract 1 from the age, otherwise 0.
function calculate_age( $date ) {
$age = date('Y') - $date;
if (date('md') < date('md', strtotime($date))) {
return $age - 1;
}
return $age;
}
echo calculate_age('1987')." years old.";
Sample output of the above program:
34 years old.
Related Articles
PHP reverse a string without predefined functionPHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Import Excel File into MySQL using PHP
PHP array length
Import Excel File into MySQL Database using PHP
PHP String Contains
PHP remove last character from string
PHP calculate percentage of total
PHP sanitize input for MySQL
Display PDF using an AJAX call
How to fetch data from database in php and display in pdf
How to read CSV file in PHP and store in MySQL
How to create a doc file using PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Sending HTML form data to an email address