PHP: Calculate people’s age from date of birth

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

When it comes to managing personal data in applications, calculating age is a common operation. In PHP, this task involves manipulating dates and understanding the DateTime class. Let’s explore various approaches to calculate age accurately.

Basic Calculation Using DateTime

Start with the core concept of calculating age in PHP using the DateTime class. This object-oriented approach provides a reliable way to work with dates:

$birthdate = new DateTime('1990-01-01');
$today = new DateTime('now');
$interval = $today->diff($birthdate);
echo $interval->y;

In this example, a DateTime object is created for the birth date, and another for the current date. The diff method calculates the difference, and the ‘y’ property gives us the total number of years, which is the age.

Handling Leap Years

Leap years add some complexity to age calculations. PHP’s DateTime takes care of leap years when using the diff method:

$birthdate = new DateTime('2004-02-29');
$today = new DateTime('now');
$interval = $today->diff($birthdate);
echo $interval->y;

Even for birth dates on February 29th, the age is calculated correctly without any additional logic required.

Using Timestamps

Another approach involves Unix timestamps, which count the seconds since January 1, 1970. Here’s how you could use them for age calculation:

$birthdate = strtotime('1990-01-01');
$age = floor((time() - $birthdate) / 31556926); // seconds in a year
echo $age;

This method has its drawbacks, like not accounting for leap seconds, but offers a quick and easy way to get an approximate age.

Working with Timezones

Timezones can affect the accuracy of your age calculations. Use DateTimeZone to ensure results are correct regardless of the user’s location:

$timezone = new DateTimeZone('America/New_York');
$birthdate = new DateTime('1990-01-01', $timezone);
$today = new DateTime('now', $timezone);
$interval = $today->diff($birthdate);
echo $interval->y;

By explicitly setting the timezone, age calculation remains consistent and accurate.

Advanced: Working with DateTimeImmutable

DateTime can have some unexpected behaviors due to its mutability. To prevent issues, consider using DateTimeImmutable:

$birthdate = new DateTimeImmutable('1990-01-01');
$today = new DateTimeImmutable('now');
$interval = $today->diff($birthdate);
echo $interval->y;

This code is very similar to our first example; however, using DateTimeImmutable ensures the objects will not change state, making your application’s logic more predictable and safer.

Custom Utility Function

To encapsulate our logic and reuse it easily, we can create a custom function to calculate age:

function calculateAge($birthdate) {
    $date = new DateTime($birthdate);
    $now = new DateTime();
    $interval = $now->diff($date);
    return $interval->y;
}
echo calculateAge('1990-01-01');

This function can now be called anywhere in your project, improving code organization and readability.

Conclusion

Having reviewed methods from basic to advanced, we’ve seen how PHP’s DateTime and DateTimeImmutable classes provide powerful and flexible tools for age calculation. Make sure to consider leap years, timezones, and whether immutability could help avoid bugs in your applications. With these tips, you’re now ready to implement robust age calculation in your PHP projects.