-1

I have this line of code. obviously it just grabs the date of birth per user and put it in the view where I want it. The problem is that it shows it in a UNIX timestamp. Is there an easy way to adjust the line so the outcome will be the age of the user?

<dl class="clearfix bg-lightgrey user-dash-dl tc">
<dd class="fl br-1 lh36" style="width:72px">
<p class="f11 ff-l lh12 pt33">Age</p>
<p class="ff-m f12"><?= $user_info['date_of_birth']?></p> </dd> </dl>

above you see the code. Thanks:)

Marten
  • 1,376
  • 5
  • 28
  • 49
sterrek
  • 57
  • 9
  • 2
    [How to calculate the difference between two dates using PHP](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Mark Baker May 24 '16 at 09:33
  • Hope this helps, you can check the answer of Dini88 in http://stackoverflow.com/questions/10186811/calculating-age-from-date-of-birth-in-php – yowza May 24 '16 at 09:38
  • Try this `

    = date('Y') - date('Y', $user_info['date_of_birth']) ?>

    `
    – yowza May 24 '16 at 09:51
  • @yowza Your solution does not consider the days. As the birthday of the user has been, there should be a year be added. – Marten May 24 '16 at 10:01
  • try this = (time() - $user_info['date_of_birth']) /(365*60*60*24) ?> – Albin Mathew May 24 '16 at 10:05
  • @Marten thanks for pointing it out what I missed! – yowza May 24 '16 at 10:09

1 Answers1

2

You can use the PHP date() function. It formats your timestamp into a human readable format. See for more information: http://www.w3schools.com/php/func_date_date.asp

<dl class="clearfix bg-lightgrey user-dash-dl tc">
<dd class="fl br-1 lh36" style="width:72px">
<p class="f11 ff-l lh12 pt33">Age</p>
<p class="ff-m f12"><?= date('Y-m-d', $user_info['date_of_birth']) ?></p> </dd> </dl>

For calculating the number of years since a specific date, use this:

echo date_diff(date_create(date('Y-m-d', timestamp)), date_create('today'))->y;

So, in your situation it should be:

echo date_diff(date_create(date('Y-m-d', $user_info['date_of_birth'])), date_create('today'))->y;
Marten
  • 1,376
  • 5
  • 28
  • 49