PHP – Calculate your age

Back in 2014 I wrote a JavaScript tutorial on calculating your age. Today, we are going to be exploring the same thing but in PHP!

Calculating the difference between two dates is much easier in PHP thanks to the DateTime family of classes introduced to the standard library in PHP 5.2.0. They provide built-in methods for timezones, date comparison, and date difference, just to name a few things. Because they abstract all the hard work for us (like good APIs ought to do), we will be putting them to use in this tutorial. If you are one of those poor souls who have to use an even older version of PHP (and I am so, so sorry about that. I hope you are paid well.), it would not be hard to port the JavaScript tutorial and replace the UTC calls with calls to date() (I recommend using “j”, “n”, and “Y”). You might also require running date_default_timezone_set("UTC"); before that depending on your server configuration.

On to the code! 😀

$now = new DateTime('now', new DateTimeZone('UTC'));

We start by creating a new DateTime instance. We will pass the string 'now' as the first parameter (the meaning is hopefully obvious). For the second parameter, we will make a new DateTimeZone instance and pass it the string 'UTC' (in this way, the calculation remains the same no matter where in the world your visitors are). Finally, we store the DateTime instance in the variable $now.

$bDay = new DateTime('1995-03-13');

Here we create another DateTime instance, passing it my birthday.

$myAge = $now->diff($bDay);

This is where the magic happens. Every DateTime instance contains a diff() method that accepts another DateTime instance as an argument. The method calculates the date difference between the two instances handling any validation checks for us) and returns an instance of the DateInterval class (yes, there is a lot of instancing going on here). We store this DateInterval instance in $myAge.

echo "<p>Caleb is {$myAge->y} years old.</p>";

Finally, a echo out a string stating old I am. In this string, we interpolate the statement $myAge->y. You see, DateInterval contains a property (among others) called y (for years). Thus all we have to do is access this property to get my current age. If we wanted, we could have accessed this property when assigning $myAge and not have to do it during interpolation.

To help you see the whole thing, here is the complete code we used.

  $now = new DateTime('now', new DateTimeZone('UTC'));
  $bDay = new DateTime('1995-03-13');
  $myAge = $now->diff($bDay);
  echo "<p>Caleb is {$myAge->y} years old.</p>";

As you can see, calculating my age in PHP was not at all that hard. Thanks to the DateTime family of classes, we completed it in a mere 4 lines. 😀

Congratulations! You can now calculate your age using PHP! Feel free to use this bit of code on your site. If you find it useful, please send me a shout out on Twitter, and do not hesitate to ask any questions you may have. 😀



Triangular Reactions

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s