Posted By

AzizLight on 10/26/09


Tagged

dynamic AGE


Versions (?)

Dynamic age


 / Published in: PHP
 

URL: http://jrtashjian.com/blog/dont-hard-code-your-age/

  1. $birth_date = getdate(strtotime("August 1st 1988"));
  2. $current_date = getdate(time());
  3. echo ($current_date['mday'] >= $birth_date['mday'] AND $current_date['mmonth'] >= $birth_date['mmonth']) ? ($current_date['year'] - $birth_date['year']) : (($current_date['year'] - $birth_date['year']) - 1);

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MMDeveloper on October 27, 2009

you can do this with one, much shorter, line of code

$yearsOld = floor(abs(mktime() - strtotime("March 17, 1982")) / 31536000);

subtracts the timestamp of the parsed birth date, from the current timestamp. It then divides that number by the number of seconds in a 365 day year. The result being the number of years between the 2 dates.

You need to login to post a comment.