-3

My Code :

<?php
  echo date('d/ m /Y', strtotime($reg_bday));
?>

How to calculate age with this function?

Vickel
  • 7,879
  • 6
  • 35
  • 56
San Anil
  • 1
  • 1
  • 3

2 Answers2

1

Short Answer :

function calc_age($date)
{
   return((int)date_diff(date_create($date),date_create('today'))->y);
}

Using :

echo calc_age("1967/03/12");

Related Links :

Max Base
  • 639
  • 1
  • 7
  • 15
0

You can use Carbon PHP class https://carbon.nesbot.com/docs/

Carbon::createFromDate(1991, 7, 19)->diff(Carbon::now())->format('%y years, %m months and %d days')

Output like "23 years, 6 months and 26 days"

Or native PHP based on another answer https://stackoverflow.com/a/3776843/5441049

<?php
  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "12/17/1983";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is:" . $age;
?> 
Mahmoud Gamal
  • 322
  • 4
  • 10