Archive for the ‘PHP & MySQL’ Category

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