1

Suppose my age on today is 20 years 11 months and 3 days 0r 20 years 3 months 15 days. I want to fetch my age with completed years. What I mean is fetch my age 20 years not 21. I want this code in PHP. What I already have is

if ($_SESSION['type_of_coverage']=='1') {
$d11=explode("/",$_SESSION['birth_date1']);
} else if ($_SESSION['type_of_coverage']=='2') {
$d11=explode("/",$_SESSION['birth_date2']);
} else if ($_SESSION['type_of_coverage']=='3') {
$d11=explode("/",$_SESSION['birth_date3']);
}

$date11 = mktime(0,0,0,$d11[1],$d11[0],$d11[2]);

$date22 = mktime(0,0,0,date("m"),date("d"),date("Y"));

$dateDiff1 = $date11 - $date22;  

echo $age  = abs(number_format(($dateDiff1/(60*60*24))/365,0));

Thanks.. pardon for my english

2 Answers2

2

You can Use this

 $ageInYears =  date_diff(new DateTime(), new DateTime($date))->format("%Y");
Orangepill
  • 24,500
  • 3
  • 42
  • 63
1
$date11 = new DateTime($d11[2].'-'.$d11[1].'-'.$d11[0]);
echo $date11->diff(new DateTime())->y;
000
  • 26,951
  • 10
  • 71
  • 101
  • Actually, I need this code in 5.2. I made like this..and work fine for me. $dateDiff1 = round($date11->format('U') - $date22->format('U')); $age = floor(abs(($dateDiff1/(60*60*24))/365)); – user2472288 Jun 11 '13 at 06:22
  • I cannot recommend any more that you try out 5.3. Most likely your codebase will still work, – 000 Jun 11 '13 at 14:02