Is your client looking for a programming or design skill that you don't have? Refer a project to us, earn up to $10,000 in referral commission.
Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Calculate age from birth date

A PHP function to calculate the age of a person by passing in their birth date in standard format.

On Friday, January 18th 2008 at 11:22 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.8 with 23 votes)
Contextual Ads
More PHP Resources
  1. <?php
  2. // Calculate the age from a given birth date
  3. // Example: GetAge("1986-06-18");
  4. function GetAge($Birthdate)
  5. {
  6.         // Explode the date into meaningful variables
  7.         list($BirthYear,$BirthMonth,$BirthDay) = explode("-", $Birthdate);
  8.         // Find the differences
  9.         $YearDiff = date("Y") - $BirthYear;
  10.         $MonthDiff = date("m") - $BirthMonth;
  11.         $DayDiff = date("d") - $BirthDay;
  12.         // If the birthday has not occured this year
  13.         if ($DayDiff < 0 || $MonthDiff < 0)
  14.           $YearDiff--;
  15.         return $YearDiff;
  16. }
  17. ?>
Digg Digg It!     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Xaeryan on Monday, February 25th 2008 at 11:50 PM

Doesnt work properly because of the if statement.

// returns years since $a_certain_date (format: YYYY-MM-DD)
public static function getUserAge ( $a_certain_date ) {
list ( $year, $month, $day ) = explode ( '-', $a_certain_date );
$year_diff = date('Y') - $year;
if ( date("m") < $month || (date("m") == $month && date("d") < $day))
return $year_diff;

by vike on Tuesday, June 17th 2008 at 05:41 PM

The code at is better.

by Ryan on Monday, June 1st 2009 at 07:18 AM

Just run the dates together and check which one is largest, its not rocket science guys

by Ryan on Monday, June 1st 2009 at 07:25 AM

function getAge($birth_date){
list($birth_year,$birth_month,$birth_day) = explode("-", $birth_date);
$year_diff=date("Y")-$birth_year; $this_birthday=date("Y").$birth_month.$birth_day;


return $year_diff;


by Bappy on Monday, April 12th 2010 at 08:36 AM

Darun, it is a bangali word

by D-Bomb on Thursday, August 19th 2010 at 10:11 PM

What do you guys think about this simple code that checks valid age.

if (checkdate($m,$d,$y)){
$min = date("Y") - 14;
$now = $min - $y;
if (($y < $min) || ($y == $min

by Bappy Saha on Tuesday, August 24th 2010 at 01:33 AM

By using this code, i can't get actual(100%) correct age.

by Eric on Friday, September 10th 2010 at 05:30 PM

I would add an extra condition
if ( ($DayDiff < 0

by Sudhir on Monday, September 20th 2010 at 08:17 AM

Easier way:

by Edward on Tuesday, March 29th 2011 at 08:12 AM

this does not explain what i want. I'm trying to calculate the year of birth by using his/her current age by the current year. So which should I use, a TextBox or a DateTimePicker.

by diamondnerd on Tuesday, August 23rd 2011 at 09:29 AM

I don't understand! I know that code is right, but when I insert it into a file, then I get "cannot redeclare getAge()". Why is that? :S

by Raizel on Thursday, October 20th 2011 at 03:00 AM


by Raizel on Thursday, October 20th 2011 at 03:00 AM


by Raizel on Thursday, October 20th 2011 at 03:00 AM


by Michal on Tuesday, January 24th 2012 at 06:22 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by Anish Mohan on Thursday, January 26th 2012 at 05:22 PM

The if condition can be updated as
if ($monthDiff < 0)
$yearDiff--;//bday didn't occur this year
else if ($monthDiff == 0)
if ($dayDiff < 0)
$yearDiff--;//bday on same month and day in future

by Marko on Thursday, February 23rd 2012 at 07:38 PM

works normaly americans

by on Friday, February 24th 2012 at 04:49 PM

There are many things should be taken into consideration, but you’ve made a good point here. Thanks a lot for that. I will follow your way soon.

by senthilvel on Friday, March 2nd 2012 at 12:04 AM

Dai mayiru mathiri answer...............................

by senthilvel on Friday, March 2nd 2012 at 12:04 AM

Dai mayiru mathiri answer...............................

by monster beats s on Tuesday, March 6th 2012 at 11:32 PM

Votre essai est bon, je suis content pour déterminer ce type d'un site Internet fantastique

by gvhghjhhuhjjhbhbhjhjhjbjnbjgbjbjbjkbjvbhb on Wednesday, July 16th 2014 at 01:30 PM


Comment Comment on this tutorial
Name: Email:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:

Advanced Search >>
Discover Geekpedia