Just found out my brother and his team ran the 3rd fastest time in the nation in the sprint medley relay!

PHP Get Age Function

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:
  • del.icio.us
  • Digg
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • NewsVine
  • Technorati
  • Reddit
  • LinkedIn

RSS feed of comments for this entry

  1. 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. Great post :) i’m using it :)

  3. 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).

    • 9.12.2008 at 5:19 pm
    • #

Speak Your Mind

* Denotes Required Field

  1. To post code snippets, use <pre><code>YOUR CODE HERE</code></pre>
Me, Trevor Davis. My blue steel face…

Hi, I’m Trevor Davis

I’m a 25 year old Front-End Developer living in Arlington, VA. I work full-time at Viget Labs and freelance on the side.

I specialize in CSS, HTML, jQuery, WordPress & ExpressionEngine. See more of my work, and then hire me.

Recent Work

  • 4th District IBEW Health Fund
  • Employers Council on Flexible Compensation
  • Monica Davis
  • The National Christmas Tree
  • Matrix Group International
  • The MatriX Files
  • Wireless Career
  • George Washington Wired
  • Direct Selling 411
  • Makeup Bizz
  • InstallNET
  • National Park Foundation
See More of My Work

Asides