Displays age from birthday

Hello

How can I display a users age from a birthday.

if I had this data:

born: 25/06/1990 13:57:39

The twist is, I don’t want just the year. I want the year, day, minute and second.

Do you want the age relative to the current time on the server your script is on or the users local time on their pc, bearing in mind that the time on the user’s pc might be wrong.

Thanks for your response.

Relative to the current time on the server

There are lots of examples on the Internet.

This sounds like a homework exercise, so post the code you have so far.

Hi, this isn’t homework. This is what I have so far, which only gives me the year.

function GetAge($Birthdate)
{
        // Explode the date into meaningful variables
       $Dateparts = explode("-", $Birthdate);
  // Find the differences
  $DayDiff =  $Dateparts[0] -  date("d");
       $MonthDiff = date("m") - $Dateparts[1];
      
       $YearDiff = date("Y") -$Dateparts[2];
   
// If the birthday has not occurred this year
if ($DayDiff < 0 or $MonthDiff < 0)  {    $YearDiff--;  }

echo $YearDiff ;
  
}
GetAge("25-06-1990");

Any clues here? [google]birthday countdown in PHP/JS[/google].

It seems you are getting the DOB from an sql table, is that so?

Are you on PHP >= 5.3?

If so, you can use [fphp]DateTime[/fphp] and [fphp]DateInterval[/fphp]


$now = new \\DateTime();
$then = new \\DateTime($dob_from_database);
$interval = $now->diff($then); // interval is a \\DateInterval object
echo $interval->format('%y year(s), %m month(s), %d day(s), %i minute(s) and %s second(s)'); 

:slight_smile: