I encountered a problem last time about solving for the age of a certain member based on the member’s birthday and the date today. I’ve found several codes that can do this but the only problem is it only displays the difference between years and excluded the month which yields a wrong result. Let’s say the current date today is April 15, 2016 and the member’s birthday is October 14, 1989. The age should be 26 and not 27.
So here is the code that solves for the right age of a member base on member’s birthday. I will explain the code for you to understand. The first date_create contains the birthday of a member while the second date_create contains the current computer date using the diff function. The diff function will solve for the difference between the two (2) dates and will return value in year.
PHP Code to get age base on birthday
<?php
$age = date_create('1989/10/14')->diff(date_create('today'))->y;
echo $age." years old";
?>
No comments:
Post a Comment