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
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
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
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');
You can use this:
<?php
$from = new DateTime('1993-09-19');
$to = new DateTime('today');
echo $from->diff($to)->y;
?>
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);
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");
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.");
?>
<?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'