Jump to content

Age from Birthday


hmvrulz

Recommended Posts

i have create a script to get the age from Birthday. here it is...

If theres any easy method or logic please let me knw

 

<?php
// Count number of days between two dates
function days($endDate, $beginDate)
{

			//explode the date by "-" and storing to array
			$date_parts1 = explode("-", $beginDate);
			$date_parts2 = explode("-", $endDate);
			//gregoriantojd() Converts a Gregorian date to Julian Day Count
			$start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
			$end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
			return $end_date - $start_date;
}


// Age from birthday
function get_age($bday)
{
			$no_of_days = days(date("Y-m-d"), $bday);
			$years = $no_of_days / 365;
			$round_off = explode(".", $years);
			return $round_off[0];


}

echo get_age('1986-06-22');

?>

Link to comment
https://forums.phpfreaks.com/topic/126974-age-from-birthday/
Share on other sites

Hmm... Thinking more about this, you're wanting to say that if someone is 24 years and 11 months old, you want it to display 24 years old and not round up to 25, which makes sense.

 

Try this:

<?php
function birthDate($bdate){
  $btime = strtotime($bdate);
  $byear = date('Y',$btime);//birth year
  $bmonth = date('m',$btime);//birth month
  $bday = date('d',$btime);//birth day
  $nyear = date('Y');//this year
  $years = $nyear-$byear;//number of years since birth year
  if (strtotime("$nyear-$bmonth-$bday")>time()){
    $years--;//subtract one year if they haven't had a birthday yet this year
  }
  return $years;
}
echo birthDate('1987-12-01');
?>

Link to comment
https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656848
Share on other sites

Hmm... Thinking more about this, you're wanting to say that if someone is 24 years and 11 months old, you want it to display 24 years old and not round up to 25, which makes sense.

 

Try this:

<?php
function birthDate($bdate){
  $btime = strtotime($bdate);
  $byear = date('Y',$btime);//birth year
  $bmonth = date('m',$btime);//birth month
  $bday = date('d',$btime);//birth day
  $nyear = date('Y');//this year
  $years = $nyear-$byear;//number of years since birth year
  if (strtotime("$nyear-$bmonth-$bday")>time()){
    $years--;//subtract one year if they haven't had a birthday yet this year
  }
  return $years;
}
echo birthDate('1987-12-01');
?>

 

thaxn for the functions and your valuable time

Link to comment
https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656864
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.