PHP calculates the current age of a person

I have dates of birth on my site in the format 12.01.1980 .

 $person_date (string) = Day.Month.Year 

Want to add an old person. Like " Currently 30 years old " (2010 - 1980 = 30 years).

But makin function only for years can not give an ideal result:

If the personโ€™s date of birth is 12.12.1980 , and the current date is 01.01.2010 , the person does not have 30 years. It is 29 years old and one month old.

When comparing the current date, targeting should be calculated for the year, month and day with the date:

0) Parse dates.

 Birth date (Day.Month.Year): Day = $birth_day; Month = $birth_month; Year = $birth_year; Current date (Day.Month.Year): Day = $current_day; Month = $current_month; Year = $current_year; 

1) year, 2010 - 1980 = write "30" (let it be $total_year variable)

2) compare months if (month of birth is greater than> current month (for example, 12 at birth and 01 at current)) {do minus one year from the variable $total_year (30 - 1 = 29)}. If minus, finish the calculations at this stage. Else go to the next (3 steps).

3) else if (birth month < current month) { $total_year = $total_year (30); } else if (birth month < current month) { $total_year = $total_year (30); }

4) else if (birth month = current month) { $total_year = $total_year (30); } else if (birth month = current month) { $total_year = $total_year (30); }

and check the day (at this step):

  if(birth day = current day) { $total_year = $total_year; } else if (birth day > current day) { $total_year = $total_year -1; } else if (birth day < current day) { $total_year = $total_year; } 

5) echo $ total_year;

My php knowledge is not very good, hope you can help.

Thanks.

+6
date php
source share
3 answers

You can use the DateTime class and diff () .

 <?php $bday = new DateTime('12.12.1980'); // $today = new DateTime('00:00:00'); - use this for the current date $today = new DateTime('2010-08-01 00:00:00'); // for testing purposes $diff = $today->diff($bday); printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d); 

prints 29 years, 7 month, 20 days

+37
source share

@VolkerK answer extension is great! I never like to see zero age, which happens if you use only a year. This function shows their age in months (if they are one month or more), and in other cases days.

 function calculate_age($birthday) { $today = new DateTime(); $diff = $today->diff(new DateTime($birthday)); if ($diff->y) { return $diff->y . ' years'; } elseif ($diff->m) { return $diff->m . ' months'; } else { return $diff->d . ' days'; } } 
+6
source share

I continued @Jonathan's answer to give a more โ€œhuman-friendlyโ€ answer.

Using these dates:

 $birthday= new DateTime('2011-11-21'); //Your date of birth. 

And calling this function:

 function calculate_age($birthday) { $today = new DateTime(); $diff = $today->diff(new DateTime($birthday)); if ($diff->y) { return 'Age: ' . $diff->y . ' years, ' . $diff->m . ' months'; } elseif ($diff->m) { return 'Age: ' . $diff->m . ' months, ' . $diff->d . ' days'; } else { return 'Age: ' . $diff->d . ' days old!'; } }; 

Returns:

 Age: 1 years, 2 months 

Cute - for really young only a few days!

+2
source share

All Articles