Avatar

How to get age from given birthdate

1
2
3
4
5
<?php
function getAge($birthdate) {
    return floor((time() - strtotime($birthdate))/(60*60*24*365.2425));
}
?>

Refactorings

No refactoring yet !

C7402070919f7a3c76fec48132ec8bd2

Alix Axel

May 3, 2010, May 03, 2010 00:26, permalink

No rating. Login to rate!

This computes the age in a non-mathematical way and thus is not subject to imprecisions caused by leap years. This is the way we (humans) celebrate birthdays, not how many (60*60*24*365.2425) seconds have passed since we were born.

1
2
3
4
5
6
7
<?php

function getAge($birthdate) {
    return intval(substr(date('Ymd') - date('Ymd', strtotime($birthdate)), 0, -4));
}

?>

Your refactoring





Format Copy from initial code

or Cancel