0

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 ?

Aaqib Mehran
  • 41
  • 1
  • 6
  • 1
    try this : https://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates – Hamza Abdaoui Oct 17 '17 at 08:23
  • 2
    Possible 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 Answers2

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;
    
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