PHP Get Age Function

Posted on February 5, 2008 in Tutorial | 4 Comments »

Such things like copyright dates, ages, etc. can cause problems on websites unless they are dynamic. For example, I have my age in the little about me blurb in the sidebar. I made a little function to calculate my age so that I don’t have to go in every year on my birthday and add another year to my age.

The Function

function age($bMonth,$bDay,$bYear) {

	$cMonth = date('n');
	$cDay = date('j');
	$cYear = date('Y');

	if(($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth)) {
		return ($cYear - $bYear);
	} else {
		return ($cYear - $bYear - 1);
	}
}

This function takes 3 arguments: the birth month, the birth day, and the birth year. It will return the calculated age. It obviously won’t work with an age in the future; it will return a negative number. I could add in checking to see if it’s a valid date, but I don’t think it’s really necessary.

Check Out Some Examples

Nothing too complicated. Let me know if anyone has any suggestions to shorten the code.

Share This:
  • NewsVine
  • Technorati
  • Reddit
  • Google
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Ma.gnolia
  • TwitThis

4 Responses

  1. KaimiteMarch 10, 2008 at 5:36 am

    Hello :

    Here is 2 suggestions :

    function age2($bDate) {
    	list($bYear, $bMonth, $bDay) = explode("-", $bDate);
    	list($cYear, $cMonth, $cDay) = explode("-", date("Y-m-d"));
    	return ( ($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth) ) ? $cYear - $bYear : $cYear - $bYear - 1;
    }
    
    echo age2("1978-01-14");

    OR

    function age2($bMonth,$bDay,$bYear) {
    	list($cYear, $cMonth, $cDay) = explode("-", date("Y-m-d"));
    	return ( ($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth) ) ? $cYear - $bYear : $cYear - $bYear - 1;
    }
    
    echo age2(1,14,1978);

    See you.

    Kaimite

  2. Sony AKJune 19, 2008 at 3:22 am

    Great post :) i’m using it :)

  3. Matthew PennellJune 21, 2008 at 10:45 am

    Sorry for the much delayed comment, but how about this much simpler solution:

    echo intval((time() - strtotime(‘14-01-1978’)) / 31557600);

    It will tick over at some point during the day of your birthday, as it uses an approximation of a year (365.25 days to represent a partial leap-year).

  4. How to take out user's AGE in PHP? - DesignersTalkSeptember 12, 2008 at 5:19 pm

    […] this PHP Get Age Function | Trevor Davis My-PHP.tk ••• Your Free PHP Resource! ••• […]

Speak Your Mind

* Denotes Required Field

  1. Sick of filling out this form? Register or Log in now.

Who Am I?

Trevor Davis I’m Trevor Davis, a 24 year old Front-End Developer. Basically, I make web sites.

By day, I work for Matrix Group International in Alexandria, VA, and by night, I freelance.

Feel free to get in touch with me about anything.

What Have I Done?

  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In

View All My Work »

Bookmarks

  • Google Search Engine Optimization Starter Guide [PDF]

    Google has released a free 22-page Search Engine Optimization Starter Guide containing plenty of well-written, practical and straightforward advice for webmasters. If you've been looking into SEO for a while it probably won't contain anything new for you, but it's useful as a set of guidelines as to what Google considers to be good optimization practice. (psst, Google, with just a little design work it could have looked so much nicer!)

  • The importance of setting expectations

    To make your customer's experience better, be sure to set their expectations.

  • XML Sitemaps Generator

    Insert your URL and let it generate the XML sitemap for you. Very useful for static websites.

  • Train-ee ExpressionEngine Training

    Learn ExpressionEngine with books, screencasts, classroom training and free tutorials from Train-ee.com

  • web.without.words

    Weekly gallery of popular websites reconstructed by removing all words and images, replacing them with blocks.

View All My Bookmarks »