Gmail / Facebook Style Chat Application
Details Live Demo Download

Posted: Saturday 27th of October 2012

Calculate Age from Birth Year via PHP

If you have a system where you need to calculate the actual age of your users from their year of birth, this tutorial brings an end to such problems.
The tutorial helps you to calculate the age of a user from a given year of birth. To calculate the age of a single user, just pass the year of birth of that user to the below function to output the user's age on the screen.
To calculate the ages of multiple users, simply pass the ages of these users in a while loop based on the year of birth of these users from your database etc, to the below function to output their ages on the screen.
Below is the age calculation function and an example of how to pass a user's year of birth to the function and display it on the screen.


Calculate User Age

$years = "1985"; //Assigned year of birth - This is the area you can pass your user's birth year

function calculateAge($birthdate)
      return floor((time() - strtotime($birthdate))/31556926);

print calculateAge($years)." year(s)"; //This one displays the user's actual age


Calculate Days Remaining Till User Birthday and also User Age

function vpb_calculate_user_birthday($birthdate)
//If the user refuses to specify a birthday, just set today as birthday by default
if(empty($birthdate)) { $vpb_birthday_year_month_day = date("Y-m-d"); }
else { $vpb_birthday_year_month_day = $birthdate; }

// Current date which is today's date
$vpb_todays_date = date('Y-m-d');

//Get the Year, Month, Day from the given date of birth
$vpb_todays_date_array = explode('-',$vpb_todays_date);
$vpb_birthday_year_month_day_array = explode('-',$vpb_birthday_year_month_day);

//Pass Year, Month, Day to one variable as the user birthdate
$vpb_formatted_birthdate = $vpb_todays_date_array[0]."-".$vpb_birthday_year_month_day_array[1]."-".$vpb_birthday_year_month_day_array[2];

$vpb_date_difference = strtotime($vpb_formatted_birthdate)-time();//Current time returns in seconds
$vpb_age_calculation = time() - strtotime($vpb_birthday_year_month_day_array[0]);

// Days remaining till user birthday
$vpb_remaining_days = floor($vpb_date_difference/round(60*60*24));

//User age
$vpb_user_age = floor(($vpb_age_calculation)/round(60*60*24*365)); //31556926

if(strtotime($vpb_formatted_birthdate) < time())
$vpb_result = "Your birthday has passed for this year";
$vpb_result .= $vpb_user_age.' Years<br>';
if($vpb_remaining_days == 0)
$vpb_result = "Today is your birthday, Happy Birthday :) <br>";
$vpb_result .= $vpb_user_age.' Years<br>';
$vpb_result .= $vpb_remaining_days.' days remaining till your birthday<br>';
$vpb_result .= round($vpb_user_age-(1)).' Years<br>';
return $vpb_result;

//Birth Date Format: YYYY-MM-DD
echo vpb_calculate_user_birthday('1982-12-21');

That's it guys...



Announcement x
Dear Users and Clients,

This is to inform you that we have recently updated our website generally.

If you experience any problem viewing any part of this website or if you notice that any of our scripts or tutorials are not functioning properly as should be, please click on the Contact Link at the top of this page to report the issue to us.

Our priority is to give you the best of service!

Thank You,
The Vasplus Team