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.