Skip to main content
Calculate Age From Date Of Birth
Calculate Age From Date Of Birth
Exact Age Calculator

Technical & Developer Tools

PHP Age Calculation Generator

Generate PHP DateTime::diff age calculation code snippets.

PHP Age Calculation Generator Inputs

Return

The code returns for the sample26

Generated code
<?php function calculateAge(string $dob, string $today = 'today', string $tz = 'UTC'): int { $z = new DateTimeZone($tz); $i = (new DateTimeImmutable($dob, $z))->diff(new DateTimeImmutable($today, $z)); return $i->y; } echo calculateAge('2000-03-10', '2026-11-29');
Step-by-step working
  1. Sample: 2000-03-10 → 2026-11-29
  2. Expected result: 26

What Is the PHP Age Calculation Generator?

A PHP Age Calculation Generator is a tool that writes PHP code that calculates age with DateTime::diff(), which returns a DateInterval with years, months and days.

PHP has had DateTime::diff() since version 5.3. The DateInterval result holds y, m and d for the calendar age and days for the total day count.

Setting a time zone avoids off-by-1-day results. The generated code uses DateTimeImmutable and an explicit zone.

How Does the PHP Age Calculation Generator Work?

The PHP Age Calculation Generator works by creating 2 DateTimeImmutable objects and reading the DateInterval from diff().

  1. Pick the output: Choose years, years-months-days or total days.
  2. Set the time zone: Choose a zone such as UTC or Europe/London.
  3. Name the function: Set a name such as calculateAge.
  4. Copy the code: Paste it into a helper file or class.

PHP Age Calculation Generator Formula

The PHP age formula is (new DateTimeImmutable($dob))->diff(new DateTimeImmutable('today'))->y.

function calculateAge(string $dob, string $tz = 'UTC'): int { $z = new DateTimeZone($tz); return (new DateTimeImmutable($dob, $z))->diff(new DateTimeImmutable('today', $z))->y; }

  • $i = date_diff(date_create('2000-03-10'), date_create('today')); echo $i->y, $i->m, $i->d;
  • $i->days → total days
  • $i->format('%y years, %m months, %d days')
Variables in the PHP Age Calculation Generator formula
SymbolMeaning
->y, ->m, ->dYears, months and days of the interval
->daysTotal days between the 2 dates
DateTimeZoneZone used to read "today"

PHP Age Calculation Generator Example

Run (new DateTimeImmutable('2000-03-10'))->diff(new DateTimeImmutable('2026-11-25')).

  1. ->y → 26
  2. ->m → 8
  3. ->d → 15
  4. ->days → 9756

The interval is 26 years, 8 months, 15 days (9,756 days).

PHP Age Calculation Generator Conversion Table

The table lists 6 PHP expressions on the interval $i and their results:

PHP DateInterval values for 2000-03-10 to 2026-11-25
ExpressionReturnsResult
$i->yCompleted years26
$i->mExtra months8
$i->dExtra days15
$i->daysTotal days9756
$i->format('%y years, %m months, %d days')Formatted text26 years, 8 months, 15 days
$i->invert1 if the end is before the start0

$i = (new DateTimeImmutable('2000-03-10'))->diff(new DateTimeImmutable('2026-11-25')).

What Are the Uses of the PHP Age Calculation Generator?

The PHP Age Calculation Generator has 4 main uses: WordPress sites, Laravel apps, forms and reports.

  • WordPress sites: Themes and plugins show member ages on profiles.
  • Laravel apps: Models add an age accessor from a birth_date column.
  • Forms: Server-side validation enforces a minimum age.
  • Reports: Admin pages list users by age band.

PHP Age Calculation Generator FAQ

Does DateTime::diff handle leap years?

Yes, DateTime::diff counts leap days in the days total and handles February 29 birth dates.

Why set a time zone?

A time zone fixes which calendar day "today" is, so the age changes at local midnight and not at server midnight.

Written and reviewed by Iqbal Hussain. Last reviewed. Calendar rules and sources: methodology.