|
Posted by Edward Vermillion on 07/07/05 00:32
On Jul 6, 2005, at 3:59 PM, Richard Lynch wrote:
> 365.24 is an appoximation.
>
> Sooner or later, it's gonna bit you in the butt.
>
> If you want somebody's age accurately, you're probably going to have
> to do
> it the hard way.
>
> Something like this might work:
>
> <?php
> $DOB = "1988-07-06";
> $now = date('Y-m-d');
> list($by, $bm, $bd) = explode('-', $DOB);
> list($ny, $nm, $nd) = explode('-', $now);
> $age = $ny - $by;
> if ($age){
> if ($bm < $nm){
> // do nothing, they were born before this month
> // so subtracting the years is correct
> }
> elseif ($nm < $bm){
> $age--; //They were born in a later month, so not quite a year
> yet
> }
> else{
> //They were born this month. Count the days.
> if ($bd < $nd){
> //Do nothing. They were born before this date.
> }
> elseif ($nd < $bd){
> $age--; //They were born later this month, so not quite a year
> yet
> }
> else{
> //It's their birthday! Go ahead and count it as a year,
> //unless you want to futz with the hour of their birth...
> }
> }
> }
> if (!$age){
> //Do whatever you want with children less than 1 year old...
> //return it as "X months" or just call it "infant" or pretend
> they're 1
> //or whatever.
> //Maybe even just use $age (which is 0) and call it done.
> }
> ?>
Actually, I was thinking it could be done inside a simple formula since
his original "formula" works on the "leap" years, ie. years divisible
by four. I guess the trick would be figuring out the actual seconds in
a year and doing most of the calculations on the unix timestamp.
Something is tickling the back of my brain on this but I can't see it
just yet. Eh...
Edward Vermillion
evermillion@doggydoo.net
Navigation:
[Reply to this message]
|