note 60386 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:34:14 +0000 |
| Subject: | note 60386 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101801@lists.php.net to get a copy of this message | ||
Note Submitter: dark at junkie dot ro
----
I've noticed many ways to calculate the actual age of a person, and many used long algorithms
or many functions and operations, which for a community webpage, using those functions, hundred and
thousand times per minute isn't quite desired. So I came up with this optimised algorithm,
which will take a mysql formatted date and calculate the exact age. (just making it a timestamp,
substracting and then dividing by 365*24*60*60 isn't enough, because there are leap years, so
at a given point your display would be erroneous by one or two days, depending on the number of leap
years in the interval).
function getage($_date) //YYYY-MM-DD (mysql format)
{
$_timestamp = strtotime($_date);
$_years = date("Y")-date("Y",$_timestamp);
if( date("md",$_timestamp) > date("md") )
return ($_years - 1);
else
return $_years;
}
Also if you wish, for coding standard purposes, you can use intval(date("md")) or
date("nd") but the way above works too. I've used date("md") to get a
number which grows exactly as the unix timestamp, by date. This way, there is no need to extract
each day or month and make an individual comparison.
Good luck.