note 81571 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 03 Aug 2008 11:55:06 +0000 |
| Subject: | note 81571 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143116@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Requirement:
From a date of birth, work out an age on a given date.
Problem:
Solutions I found were not accurate enough, not incorporating leap years.. and often up to a week
out for a 30 year old.
Solution:
While the following still probably has it's flaws, it worked for me to enough precision.
<?php
//Get the date of birth as timestamp from american format
$birthtime = strtotime( $birth_month. "/" . $birth_day . "/" . $birth_year );
//Get the required date as timestamp (or could do time() for today)
$reqtime = strtotime( $req_month. "/" . $req_day . "/" . $req_year );
//Simple Calculation to get age
$calc_age = floor ( ($reqtime - $birthtime) / 60*60*24*365.25) ) ;
?>
Testing:
I've tested this for those aged 18 to 90. And it is accurate to the day. Obviously it
doesn't take time of birth into account.. but really.. who cares?