I have birth date of user in ('Y-m') format in database and I want to calculate his age in months and in Years in php. How to do it ?
Asked
Active
Viewed 99 times
0
-
1try this : https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates – Hamza Abdaoui Oct 17 '17 at 08:23
-
2Possible duplicate of [Finding the number of days between two dates](https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates) – teeyo Oct 17 '17 at 08:29
-
Will that work for Y-m format as well ? I don't have date in birthdate column – Aaqib Mehran Oct 17 '17 at 08:30
2 Answers
3
you can use php datetime object for it
- create a datetime object from the birthday
- create a datetime object from today date
find the difference
$today = new DateTime(); $birthday = new DateTime('1994-11-27'); $age = $today->diff($birthday); $ageString = $age->format('Y-m-d'); echo $ageString;

shan pramuditha
- 95
- 7
2
you can get difference by converting you date time to date time object and than here is code which take differnce
$interval = $timeNow->diff($previousCheckOut[0]->getCheckOut());
$differnce = $interval->format('%H:%i:%s');
$differnceDateTimeObj = new \DateTime($differnce);
let me know if you need any help

M Maavia
- 340
- 1
- 9
-
-
in above code timenow is dattime object and its is it buildin functions – M Maavia Oct 17 '17 at 08:39
-
-
"$previousCheckOut[0]->getCheckOut()" give me dattime object time now is also a datetime object – M Maavia Oct 17 '17 at 09:45