Results 1 to 6 of 6

Thread: PHP to determine a person's age

  1. #1
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default PHP to determine a person's age

    I have a person's birthdate in MySQL format: YYYY-MM-DD

    I would like to determine their age in years.

    How can I do this using PHP so I can make comparisons or print it on screen?

    Thanks a lot!

    JasonD

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I kept working on this after I posted the question and I have come up with this:

    Code:
    function age($year, $month, $day) {
    
    	$dob_time_stamp = mktime(0, 0, 0, $month, $day, $year);
    
    	$seconds_alive = time() - $dob_time_stamp;
    
    	$age = floor($seconds_alive / 31556926);
    
    	return $age;
    }
    Are there any problems with it?

    Thanks a lot!

    Jason

  3. #3
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    I don't see any problems but I'm not a PHP pro by any means. I just wanted to point you towards this page that shows multiple ways of doing this.

    Depending on what you're doing, they might be more useful as you're able to submit a full string (YYYY-MM-DD, etc...) instead of splitting it up. But that all depends on how you have them saved in the first place. Maybe your way makes more sense to your application.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That seems fine. You might run into slight problems when dealing with leap years, though that's probably not going to be a big problem (you might get someone's age wrong by one year for one day, or something).

    Using timestamps as a base is the best, because you can, from those, by design, easily get the date (in various formats), etc.
    Using a string is limited because you need to work to parse the string.

    Though, I guess, some people are older than 1970, so things might be messy.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I struggled with the decision of how to store dates of birth in MYSQL.

    On one hand unix timestamps are better, on the other hand MySQL's date functions won't work with unix timestamps. I read somethiing that made be decide to go with the MySQL date format for storing dates in MySQL and then worry about changing it in the PHP script.

    With that being said, I am not convinced this is the best way to do it. Any suggestions?

    I think you can query the MySQL date as UNIX_TIMESTAMP or something, but I am not sure how it returns the timestamp when the date is prior to 1970.

    Thanks for looking at this. Any and all comments are appreciated. Especially critical ones.

    JasonD

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    I just tested this one from dzone and I like it the best. It does take the native MySQL date string as input. Perfect for me.

    Code:
    function get_age( $p_strDate )
    {
    list($Y,$m,$d)    = explode("-",$p_strDate);
    return( date("md") < $m.$d ? date("Y")-$Y-1 : date("Y")-$Y );
    }
    Thanks for the responses!

    Jason

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •