2010-07-26 6 views
6

Ich möchte das Alter der Person in Monaten plus Tage mit Geburtsdatum berechnen (Beispiel: 1986-08-23).PHP: Wie berechnet man das Alter der Person in Monaten + Tagen?

Zum Beispiel:

0 months and 25 days old. 
5 months and 20 days old. 
150 months and 4 days old. 
285 months and 30 days old. 

eine Idee? Vielen Dank.

+0

Mit welchem ​​Wert fangen Sie an? – alex

+0

Was ist die Eingabe? Sekunden, Minuten, Tage, Jahre + Tage? Es ist schwer, eine Antwort ohne genügend Informationen zu finden. – martiert

+0

@alex: Geburtsdatum (Beispiel: 1986-08-23) – NAVEED

Antwort

17
$date = new DateTime('1990-10-13'); 
$diff = $date->diff(new DateTime()); 
printf("%d months and %d days old", $diff->y*12 + $diff->m, $diff->d); 

Beachten Sie, dass DateTime::diff() PHP 5.3.0 oder höher erfordert.

+0

Wird dies genau WRT Schaltjahre sein? – Piskvor

+0

@Piskvor Ja, wird es. –

+0

Vielen Dank für die Klarstellung. – Piskvor

Verwandte Themen