a:2:{i:0;a:1:{s:4:"data";a:1:{s:7:"entries";a:1:{i:0;a:7:{s:2:"id";s:3:"243";s:5:"title";s:20:"PHP Get Age Function";s:11:"displayDate";s:10:"02.05.2008";s:13:"attributeDate";s:10:"2008-02-05";s:4:"body";s:1288:"

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.

";s:13:"numberOfLikes";N;s:10:"bodyBlocks";a:0:{}}}}}i:1;O:25:"yii\caching\TagDependency":3:{s:4:"tags";a:4:{i:0;s:7:"element";i:1;s:29:"element::craft\elements\Entry";i:2;s:40:"element::craft\elements\Entry::section:4";i:3;s:7:"graphql";}s:4:"data";a:4:{s:40:"CraftCMSce35088bdfe0816226cd17fd051a4803";s:21:"0.70573500 1736501960";s:40:"CraftCMS2743a789e8993267a348eee1ee7e4450";s:21:"0.67537500 1713205687";s:40:"CraftCMS2ac34726f299f7e18e449d8e536c67f8";s:21:"0.84529700 1741778847";s:40:"CraftCMS3817d4a648fcfac939af46605323feb0";s:21:"0.36746500 1735923287";}s:8:"reusable";b:0;}}