{"authorId":1,"title":{"title":{}},"slug":"php-get-age-function","postDate":1202247917,"expiryDate":null,"enabled":1,"fields":{"2":"<p>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&rsquo;t have to go in every year on my birthday and add another year to my age.<\/p>\n\n<!--more-->\n\n<h2 class=\"replaceHeadline\">The Function<\/h2>\n<div class=\"codeblock\"><code>\nfunction&nbsp;age($bMonth,$bDay,$bYear)&nbsp;&#123;<br \/>&nbsp;<br \/>&nbsp;$cMonth&nbsp;=&nbsp;date('n');<br \/>&nbsp;$cDay&nbsp;=&nbsp;date('j');<br \/>&nbsp;$cYear&nbsp;=&nbsp;date('Y');<br \/><br \/>&nbsp;if(($cMonth&nbsp;&gt;=&nbsp;$bMonth&nbsp;&amp;&amp;&nbsp;$cDay&nbsp;&gt;=&nbsp;$bDay)&nbsp;||&nbsp;($cMonth&nbsp;&gt;&nbsp;$bMonth))&nbsp;&#123;<br \/>&nbsp;&nbsp;return&nbsp;($cYear&nbsp;-&nbsp;$bYear);<br \/>&nbsp;&#125;&nbsp;else&nbsp;&#123;<br \/>&nbsp;&nbsp;return&nbsp;($cYear&nbsp;-&nbsp;$bYear&nbsp;-&nbsp;1);<br \/>&nbsp;&#125;<br \/>&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>This function takes 3 arguments: the birth month, the birth day, and the birth year. It will return the calculated age. It obviously won&rsquo;t work with an age in the future; it will return a negative number. I could add in checking to see if it&rsquo;s a valid date, but I don&rsquo;t think it&rsquo;s really necessary.<\/p>\n\n<p><a href=\"http:\/\/trevordavis.net\/play\/php-get-age-function-examples\/\">Check Out Some Examples<\/a><\/p>\n\n<p>Nothing too complicated. Let me know if anyone has any suggestions to shorten the code.<\/p>","4":{"website":{}}}}