How to calculate age from a birth date
Short answer
The diff() method returns the interval between dates; the y property is the number of full years:
<?php
$birth = new DateTimeImmutable('1990-06-15');
$today = new DateTimeImmutable('2025-01-30');
echo $birth->diff($today)->y;Output:
34Checking adulthood
Compare the number of years with the threshold:
<?php
$birth = new DateTimeImmutable('2010-03-01');
$today = new DateTimeImmutable('2025-01-30');
var_dump($birth->diff($today)->y >= 18);Output:
bool(false)Functions used in this recipe
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.
- checkdate — Checks whether such a date exists.