Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to calculate person's age in months+days?

Tags:

date

php

I want to calculate person's age in months plus days using date of birth (example: 1986-08-23).

For example:

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

Any Idea? Thanks.

like image 466
Naveed Avatar asked Jul 26 '10 07:07

Naveed


People also ask

How do you convert age into months and days?

Age Calculation in Months NOTE: We divide the days by 30.5 because months alternate with 30 and 31 days. Hence the age in months for the given date of birth is 415 Months 16 Days since the DOB 26-08-1980 as of 11-05-2015.

How do I calculate age in months and days in MySQL?

In MySQL, you can use the inbuilt TIMESTAMPDIFF() function to compute the difference between two dates and return a value either in days, months or years. See the basic syntax below. mysql> SELECT TIMESTAMPDIFF(MONTH, FIRST_DATE, SECOND_DATE);

What is the formula to calculate age in days?

Basic Excel formula for age in years This conventional age formula can also be used in Excel. The first part of the formula (TODAY()-B2) returns the difference between the current date and date of birth is days, and then you divide that number by 365 to get the numbers of years.


1 Answers

$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);

Note that DateTime::diff() requires PHP 5.3.0 or higher.

like image 90
Daniel Egeberg Avatar answered Oct 17 '22 00:10

Daniel Egeberg