How to calculate age based on date of birth using PHP

So a while back, I created my “About Me” page and included a bit about my family. I didn’t want the page to get out of date with ages and years married, so I found some PHP code that calculated age based on date of birth, modified it quite a bit so it would work for my needs and now my “About Me” page is always up-to-date… as far as my kids ages and the number of years I’ve been married are concerned.

I thought I would share the code here in case anyone else is looking for something similar. If you end up using the code, if you wouldn’t mind leaving me a quick comment to let me know, that would be great!
—————

//date in mm/dd/yyyy format
$mybirthDate = "8/12/1977";
$ebirthDate = "6/22/2007";
$sbirthDate = "12/18/2010";
$cbirthDate = "5/20/2014";
$anniversaryDate = "7/2/2005";

//explode the date to get month, day and year
$mybirthDate = explode("/", $mybirthDate);
$ebirthDate = explode("/", $ebirthDate);
$sbirthDate = explode("/", $sbirthDate);
$cbirthDate = explode("/", $cbirthDate);
$anniversaryDate = explode("/", $anniversaryDate);

//get age from date or birthdate
$myage = (date("md", date("U", mktime(0, 0, 0, $mybirthDate[0], $mybirthDate[1], $mybirthDate[2]))) > date("md") ? ((date("Y")-$mybirthDate[2])-1):(date("Y")-$mybirthDate[2]));
$eage = (date("md", date("U", mktime(0, 0, 0, $ebirthDate[0], $ebirthDate[1], $ebirthDate[2]))) > date("md") ? ((date("Y")-$ebirthDate[2])-1):(date("Y")-$ebirthDate[2]));
$sage = (date("md", date("U", mktime(0, 0, 0, $sbirthDate[0], $sbirthDate[1], $sbirthDate[2]))) > date("md") ? ((date("Y")-$sbirthDate[2])-1):(date("Y")-$sbirthDate[2]));
$cage = (date("md", date("U", mktime(0, 0, 0, $cbirthDate[0], $cbirthDate[1], $cbirthDate[2]))) > date("md") ? ((date("Y")-$cbirthDate[2])-1):(date("Y")-$cbirthDate[2]));

$yearsmarried = (date("md", date("U", mktime(0, 0, 0, $anniversaryDate[0], $anniversaryDate[1], $anniversaryDate[2]))) > date("md") ? ((date("Y")-$anniversaryDate[2])-1):(date("Y")-$anniversaryDate[2]));

echo "\nI'm a father of three awesome kiddos (2 girls and a boy) and husband to my beautiful wife of ".$yearsmarried;
echo " years. I am ".$myage;
echo " years old, my oldest daughter is ".$eage;
echo " and my youngest daughter is ".$sage;
echo ". My son is ".$cage;
echo ". They keep me quite busy.\n";

—————
The output for the code above is:

I’m a father of three awesome kiddos (2 girls and a boy) and husband to my beautiful wife of xx years. I am xx years old, my oldest daughter is x and my youngest daughter is x. My son is x. They keep me quite busy.

For the purposes of this post, I replaced the actual years with x’s above. To get the PHP to work in posts or on pages you must install a plugin that will do this. I am using a plugin called “Allow PHP in Posts and Pages”. It hasn’t been updated in over 4 years, but it still works with the most current version of WordPress (WP 4.7.3 as of the typing of this post). This plugin allows PHP in posts and pages by adding a shortcode [php] your code here [/php].

Funny enough, because that plugin is installed I had to write more code just to display the “left square bracket php right square bracket”.

Here’s that code:

echo "\x5b", "php", "\x5d", " your code here ", "\x5b", "/php", "\x5d";

The x5b and x5d are the unicode shortcodes for left square bracket and right square bracket.

I found some good info about PHP output here.

Even though I don’t really understand what is going on in the code calculating age I didn’t find it to difficult to edit the code. But that is the beauty of coding. I know just enough about coding to find someone else’s code and make it do what I want it to do. Anyone with the desire to learn how to code can do it. There is enough information on the Internet about coding that anyone can learn it.

Leave a Reply