note 60386 added to function.date
| From: | dark at junkie dot ro | Date: | Thu, 05 Jan 2006 15:19:35 +0000 |
| Subject: | note 60386 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101251@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60386
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60386&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60386&report=yes
Search -- http://master.php.net/manage/user-notes.php