0

Here is my code:

<?php 

            $userDob = $result['DoB'];

            //Create a DateTime object using the user's date of birth.
            $dob = new DateTime($userDob);

            //We need to compare the user's date of birth with today's date.
            $now = new DateTime();

            //Calculate the time difference between the two dates.
            $difference = $now->diff($dob);

            //Get the difference in years, as we are looking for the user's age.
            $age = $difference->y;

            //Print it out.
            echo $age;

            ?>

I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.

  • Look at the formats available in the [php manual](https://www.php.net/manual/en/function.date.php). You can output in years, months, days, hours, seconds, weeks – Michel Jun 06 '20 at 08:22

1 Answers1

0

Yes php has something built in for that :)

$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');