What’s my age again?

Posted: March 8, 2011 by yanenrogne in PHP & MySQL, Programmation

Here’s a first “code snippet” that I will be sharing. It is only a quick math to guess the age of a person with his/her birth date. I was working on PHP with a MySQL database.

The format used for the birth date was in a form with three inputs, one for year, month and day

<?php
$age = (date("Y") - (int)$_POST['year']);

if(date("m") < (int)$_POST['month'] )
$age--;
else if( date("d") < (int)$_POST['day'] )
$age--;
?>

The first part will get current year and subtract the birth date’s year so we get the age.

But this will only work if the person had his birthday already this year, that’s what the other parts are for, they will subtract one year if it haven’t happened yet!

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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