-1

I'm building a small site and got stuck on something. How do you get a persons age, using their birth date?

So this should be something like:

$age=curr_date - YEAR($birth_date)

I hope the question is clear enough.

Thanks in advance

7 Answers7

4

Use date and strtotime for getting the age of a person from birthdate.

$birth_date = '1991-12-10';
echo $age= date("Y") - date("Y", strtotime($birth_date)); //25
3

Following is the code for that:

$dob = new DateTime('2015-10-02');
$today = new DateTime;
$age = $today->diff($dob);

And you can echo out the age like this:

echo $age->format('%y Years, %m Months and %d Days');
Rahul M
  • 1,498
  • 12
  • 19
1

You can use this:

<?php
$from = new DateTime('1993-09-19');
$to   = new DateTime('today');
echo $from->diff($to)->y;
?>
aarju mishra
  • 710
  • 3
  • 10
0

First convert your date in strtotime like this

$userDob  = '18/01/2000'; //Try to convert your date format like this
$userDob  =  strtotime($userDob);
$currDate =  time();
$dateDiff =  $currDate  - $userDob;
echo Date('y',$dateDiff);
Akshay Sharma
  • 1,042
  • 1
  • 8
  • 21
0

Assuming $birth_date is in YYYY-MM-DD format.

You can do it in following way. First create DateTime object of BirthDate,

$date=date("Y-m-d",strtotime($birth_date));
$bDObj=new DateTime($date);
$cDate=new DateTime();

$age=$cDate->format("Y")-$bDObj->format("Y");
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
0

Use the DateInterval Class?

<?php
$date1=date_create("2013-01-01");
$date2=date_create("2013-02-10");
$diff=date_diff($date1,$date2);

echo $diff->format("Total number of years: %y.");
?> 
Dfaure
  • 564
  • 7
  • 26
0
    <?php

        // ASSUMING YOU HAVE THE DATE IN MYSQL FORMAT: Y-m-d
        $birthDate  = "1980-10-05";
        $today      = date("Y-m-d");

        $dateA      = new DateTime($today);
        $dateB      = new DateTime($birthDate);

        $dateDiff   = $dateA->diff($dateB);
        $age        = "{$dateDiff->y} Years, {$dateDiff->m} Months and {$dateDiff->d} Days";

        var_dump($age);     //<== DUMPS: '35 Years, 8 Months and 3 Days'
Poiz
  • 7,611
  • 2
  • 15
  • 17