[FX.php List] PHP Calculated Age
Derrick Fogle
derrick at fogles.net
Mon Nov 6 08:35:28 MST 2006
It depends on how accurate you want to be, and how old you need to
calculate ages on. Unixtime only goes back to Jan 1 1970. Using
unixtime may not work correctly when working with birthdates before
1/1/1970. Then the other thing to consider is leap years and stuff
like that.
The only way I've found to consistently produce correct ages (down to
the day) is to compare each date component (YMD ) separately:
First subtract the DOB year from the deadline year. That will always
yield an age that is either correct or one year too old depending on
whether the actual birthdate has passed yet.
Then compare the month: if the DOB month is greater than deadline
month, subtract a year from the age found from the first calculation.
Finallly, if the month of both dates is the same, you then need to
compare the day of month: if the DOB day is greater than deadline
day, subtract a year from the age found in the first calc.
In FileMaker, the calculation is this:
(Year(Get(CurrentDate)) - Year(Birthday)) -
Case(
Get(CurrentDate) < Date(Month(Birthday); Day(Birthday); Year(Get
(CurrentDate)));
1
)
Unfortunately, I seem to have lost my PHP-based age calculation. I
haven't had to deal with age determination for a couple of years now
(thank goodness!), and have changed jobs and computers since then.
Also, I don't know where the date info is coming from - user input on
the web? Do you have those fields broken down into M, D, and Y
fields? The Reference date coming from FMP as a text string? All
those make a difference on how you end up dealing with the dates and
returning an age in PHP.
On Nov 5, 2006, at 12:25 PM, Jonathan Schwartz wrote:
> Hi Folks,
>
> Can't seem to get a handle on how to have PHP calculated an age
> based on birthdate and a particular deadline (April 30 ,2007)...and
> not the current date.
>
> So if a user was born on April 1, 2000, the age should be 6. If
> the person was born on May 1, 2000,the age would be 7. Just need
> the single digit.
>
> The problem is the data math, not the logic. Going a round in
> circles between date() time() and timestamp().
>
>
>
> Jonathan
> --
>
> Jonathan Schwartz
> FileMaker 8 Certified Developer
> Associate Member, FileMaker Solutions Alliance
> Schwartz & Company
> jonathan at eschwartz.com
> http://www.eschwartz.com
> http://www.exit445.com
> 415-381-1852
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
Derrick Fogle
derrick at fogles.net
More information about the FX.php_List
mailing list