Dynamic ages in About Me page

Standard
Share

Recently my brother asked me if my daughters’ ages in my About Me page were static or if they would change as we moved through time.  The answer is that they’re the result of PHP functions and are therefore dynamic and will change as time passes.

You see, my site allows me to embed PHP code.  In a nutshell, PHP allows me to create HTML dynamically.  I wrote a PHP function that calculates the number of years/months (or months/days in certain situations) since a given birthdate.  I added this function to my site’s custom functions.php file, and registered it with the WordPress framework so that I can access it with a shortcode. Because I want to be able to use this function multiple times with various birthdates, I wrote it to accept an argument called “birthdate”. In my About Me page, anytime I want somebody’s age, I simply insert:

[calcAge birthdate=”YYYY-MM-DD”]

substituting a person’s actual birthdate in YYYY-MM-DD format.

I started by adding a functions.php file to my child theme’s folder (my theme is based on the Catch Box theme, so I made a child theme that inherits from it for customization purposes). If your theme already contains a functions.php file, simply add this code to it (sans the opening and closing <?php ?> tags, which you should already have). In this functions.php file, I added the following code:

<?php
function calcAge($parameters)
{

extract(shortcode_atts(array('birthdate' => '1970-11-24',), $parameters));

$diff = abs(strtotime(date('Ymd')) - strtotime($birthdate)); 
$years = floor($diff / (365*60*60*24)); 
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); 
$results = $years." years, ".$months." months";
if($years < 2)
{
$results = $months." months, ".$days." days";
}
return $results;
}

add_shortcode('calcAge', 'calcAge');
?>

Let’s break this down, line by line.

<?php
&#91;/code&#93;
&#91;code firstline="20"&#93;
?>

Lines #1 and #20 simply start and end the PHP code. These are the default tags that define the code nested inside them as PHP.

function calcAge($parameters)

Line #2 defines my function name and the parameters it accepts. We’re simply saying here that we want to create a function called “calcAge” that accepts a variable that we’ll call “$parameters”.

Lines #3 and #17 are simply the curly braces that contain our function. All PHP functions are contained within curly braces. Lines #4, #6, and #18 are blank lines that I left in to help keep things legible. They also happen to separate my WordPress-specific code from the real “meat” of the PHP function. PHP ignores these white spaces.

extract(shortcode_atts(array('birthdate' => '1970-11-24',), $parameters));

Line #5 converts the arguments passed via the WordPress shortcode (more on that later) to variables we can use inside our function. WordPress passes an array of information to the function when we call it via the WordPress shortcode. We use this array to get the $birthdate value passed by the call to the function.

What we’re doing here is extracting (extract) from the kvp array $parameters the key-value pair (kvp) where the key = “birthdate”. The key becomes a PHP variable with the kvp value assigned. As demonstrated above in the example use of the shortcode, we’re using “birthdate” as an argument in the call to the shortcode. The use of => ‘1970-11-24’ defines a default value for the $birthdate variable in the event no $birthdate argument is passed in the call to the shortcode.

Now that we’ve set up our environment, let’s get to the meat of the function.

$diff = abs(strtotime(date('Ymd')) - strtotime($birthdate));

In line #7, we’re defining a variable called $diff, and setting its value as the number of seconds between today and the $birthdate. The result is the number of seconds of life (give or take a day).

$years = floor($diff / (365*60*60*24));

In line #8, we’re defining a variable called $years, and settings its value as the number of full years of life. This is calculated as seconds of life ($diff) divided by the (number of days/per year (365) * number of seconds/minute (60) * the number of minutes/hour (60) * the number of hours/day (24)). The PHP function floor simply rounds the results of this division down, if necessary.

$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));

In line #9, we’re defining a variable called $months, and setting its value as the number of months of life not encapsulated in a full year of life. That is to say, the number of full months remaining when you subtract the value of the $years variable (in seconds) from the value of the $diff variable. This is calculated by first converting the $years variable value to seconds ($years * 365 * 60 * 60 * 24), subtracting from the value of $diff, dividing this difference by the number of seconds in a month (30 * 60 * 60 * 24), then rounding down (floor).

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

In line #10, we’re taking it one step further, defining a variable called $days and setting its value as the number of days of life not encapsulated in a full month of life. That is to say, the number of full days remaining when you subtract the value of the $months variable (in seconds) from the value of the $years variable (in seconds) from the value of the $diff variable. This is calculated by first converting the $months variable to seconds ($months*30*60*60*24), subtracting this from the value of the $years variable in seconds ($years * 365*60*60*24), subtracting this from the value of the $diff variable, dividing this difference by the number of seconds in a day (60*60*24), then rounding down (floor).

At this point, we’ve broken up the number of seconds of life ($diff) into the number of full years of life ($years), the number of full months of life ($months), and the number of full days of life ($days).

$results = $years." years, ".$months." months";

In line #11, we’re simply defining a string by concatenating (.) the $years variable value followed by the text ” years, ” with the $months variable value followed by the text ” months”. We then store this value in a variable called $results.

It seemed kind of silly to me to report my infant daughter’s age in years and months when reporting it in months and days seemed more appropriate. Therefore, I added the following:

if($years < 2)
{
$results = $months." months, ".$days." days";
}
&#91;/code&#93;

In line #12, we're checking to see if the value stored in the $years variable is less than 2. If it is, then we redefine the value stored in the $results variable as the string obtained by concatenating (<strong>.</strong>) the number of months (<strong>$months</strong>), the text " months, ", the number of days (<strong>$days</strong>), and finally the text " days".  This <em>if</em> statement allows my daughter's age to automatically convert from a months/days format to a years/months format upon her second birthday.

[code firstline="16"]
return $results;

In line #16, we’re returning the value stored in the $results variable to the call to the shortcode.

add_shortcode('calcAge', 'calcAge');

In line #19, we’re simply registering our function with the WordPress framework as a shortcode by calling WordPress’s add_shortcode function, defining what the text of our shortcode (the first instance of ‘calcAge’), followed by the PHP function we want called by the use of that shortcode (the second instance of the ‘calcAge’).

We can now simply obtain anyone’s age in the site by placing a call to the PHP function calcAge by using the WordPress shortcode calcAge and passing a birthdate value as such:
[calcAge birthdate=”2002-07-04″]

To summarize, we added a file to our child theme folder to hold custom functions, and added to this file a PHP function that calculates a person’s age based on a given birthdate. Furthermore, we registered this function with WordPress via its “add_shortcode” function so that we can easily call this PHP function by using the WordPress shortcode calcAge.

UPDATE
This didn’t become apparent to me until after my youngest daughter had her first birthday. My wife pointed out that our daughter was 13 months and 25 days (at the time of this discovery), but the website was reporting her age as 1 months and 25 days. As it turns out I was missing a calculation in line #14 that failed to consider the months lived in the first full year:

$results = $months." months, ".$days." days";

should actually be:

$results = ($years * 12) + $months." months, ".$days." days";

Leave a Reply

Your email address will not be published. Required fields are marked *