Loading...
 
Gmail / Facebook Style Chat Application


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.

Code:

Calculate User Age

<?php
$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

<?php
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>';
}
else
{
if($vpb_remaining_days == 0)
{
$vpb_result = "Today is your birthday, Happy Birthday :) <br>";
$vpb_result .= $vpb_user_age.' Years<br>';
}
else
{
$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...


Views Today: 0
Total Views: 1153

Comments
0
OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Copyright © 2011 - 2024 | All Rights Reserved