Monday, February 27, 2012

Calculates the age in php

This simple function calculates the age, in years, of a date supplied in any format supported by the strtotime() funcition, which means, basically, any date you can sanely imagine.

Code of function is:

[php]
<?php
/*** @return int ***/

function yearsOld($birthday)
{
if (($birthday = strtotime($birthday)) === false)
{
return false;
}
for ($i = 0; strtotime("-$i year") > $birthday; ++$i);
return $i - 1;
}



/*** example usage ***/
$birthday = 'april 20 1961';

echo yearsOld($birthday).' years old';

?>

[/php]

2 comments:

  1. I simply want to mention I am just beginner to blogs and definitely loved you're website. Probably I’m going to bookmark your website . You surely come with outstanding writings. With thanks for revealing your web page.

    ReplyDelete
  2. I simply want to say I am beginner to blogging and truly savored your blog. Likely I’m likely to bookmark your blog post . You absolutely have fabulous stories. Regards for sharing with us your website.

    ReplyDelete

Note: Only a member of this blog may post a comment.