Page 1 of 1

help with my php function for determining a person age

Posted: Sat Nov 26, 2011 6:49 am
by andreea115
hi everyone

i have a script for determining a users date of birth. i feed the date of birth of a user, then convert this into a unix timestamp. i then compare this date against the unix timestamp for the current date. i then divided the two dates which gives me the age of the user. it not a hundred percent accurate becuase it does not take into account Leap years, however its good enough for my purposes.


the problem i have is when it comes to converting this script into a function. i am having problems converting the users DOB into a timestamp. it might be easier at this point if i show you the problem.

Code: Select all

                          //THE DOB FUCTION 

                               //I FEED  THE FUCNTION 3 PARAMETRES  I.E THE  DOB OF USER:        6 , 17, 1989  

           function bithday2 ( $timestamp1, $timestamp2,    $timestamp3        )
			
			  {
			       // I then attempt to convert teh dates into a UNIX Time Stamp.    ( i place  0,0,0 at the front of the converion-i did this buecase 
                               //a unix timestamp need 6 parametres THEN ATTETHEN AI THEN 
                       $TIMESTAMP       =  " 0,0,0, $timestamp1. ,  .$timestamp2 . , .$timestamp3" ; 
			   $ageTime = mktime( $TIMESTAMP  ); // Get the TIMESTAMP for the birthday		
			$t = time(); // Store current time for consistency
				$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
				$year = 60 * 60 * 24 * 365;
				$ageYears = $age / $year;
				   $birthdate  = floor($ageYears) ;
				     return $birthdate; 
					}
	  echo bithday2 ( 06 , 17, 1989    );
	
however, my attempt to convert the DOB into a TIMeSTamp FAILED. IT PRODUCED THE FOLLOWING ERROR REPORT
Notice: A non well formed numeric value encountered in C:\wamp\www\aupair-world-agency.com\cms\sql_fns.php on line 110


i have obviosuly not done the concanternation properly. i am not sure how to place the ( , )' comma , between teh variables . however. apart from this problem the script does indeed work. i enclose below a copy of the script (outside of the fuction. you will note, if you test it, that it does indeed work.

so where have i gone wrong. thanks everyone for your kind help

Code: Select all

  

function bithday (   )
			
			  {
			$ageTime = mktime(0,0,0,  06  ,17 , 1982  ); // Get the person's birthday timestamp
			$t = time(); // Store current time for consistency
				$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
				$year = 60 * 60 * 24 * 365;
				$ageYears = $age / $year;
				   $birthdate  = floor($ageYears) ;
				    return $birthdate; 
					}
		 echo bithday (   );


Re: help with my php function for determining a person age

Posted: Sat Nov 26, 2011 7:39 am
by twinedev
The main problem is that you are passing mktime() a single string, not a set of parameters that are integers for each one.

I prefer not to use timestamps for determining differences, as you said, leap years will mess you up.

Here is a script that will work for you, it is one I came up with a few years ago. The only messing with timestamp is to check to see if you entered a day of the month that is more that the number of days in the given month/year.

Code: Select all

function getAge($intMonth,$intDay,$intYear) {
    $intCMonth = date('m');
    $intCDay = date('d');
    $intCYear = date('Y');
    if ($intYear<100) { // Handle if they entered 2 digit yerar.
        if (2000+$intYear<=$intCYear) {
            $intYear += 2000;
        }  else {
            $intYear += 1900;
        }
    }
    // Check to make sure Month/Day values are in range. 
    if ($intMonth<1 || $intMonth>12) { return FALSE; }
    if ($intDay<1 || date('m',mktime(0,0,0,$intMonth,$intDay,$intYear))!=$intMonth) { return FALSE;
    $intDiff = $intCYear-$intYear;
    if ($intMonth>$intCMonth || $intMonth==$intCMonth && $intDay>$intCDay) { $intDiff--; }
    // If they entered a date in the future, at this point $intDiff will be < 0
    return ($intDiff>=0) ? $intDiff :  FALSE;
}
Note, this will accept both two digit and 4 digit years. Anything two digit is assumed to be between the current year and back 99 years (ie, for right now when I posted this, 1912-2011)

A return of FALSE indicates not a valid date (in the future, month or day of month out of range) Be sure to compare with === for false:

Code: Select all

$intAge = getAge(8,8,72);
if ($intAge===FALSE) {
   echo "You tried an invaid date\n";
} else {
   echo "Greg is $intAge year",(($intAge==1)?'':'s')," old!\n";
}

Re: help with my php function for determining a person age

Posted: Sat Nov 26, 2011 9:40 am
by Celauran
Here's a simple way of doing it:

Code: Select all

function getAge($birthdate)
{
    $today = new DateTime();
    $bdate = new DateTime($birthdate);
    $age   = $today->diff($bdate)->format('%Y');
    return $age;
}

Re: help with my php function for determining a person age

Posted: Sun Nov 27, 2011 2:41 am
by twinedev
Celauran wrote:Here's a simple way of doing it:
A note on that way is that it requires php >= 5.3.0 , but yes it is cleaner. (I had that function since prior to PHP 5)

Re: help with my php function for determining a person age

Posted: Sat Dec 10, 2011 12:00 am
by andreea115
hello everyone

i wish to first thank everyone for their kind help in this matter.

i refer to this function

function getAge($birthdate)
{
$today = new DateTime();
$bdate = new DateTime($birthdate);
$age = $today->diff($bdate)->format('%Y');
return $age;
}

i am a bit confused about this function. i.e what dates do i insert into the function. do i insert a unix timestamp or the the full date of birth. i tried inserting the bithdate but the fuction did not work .

warm regards

Andreea

Re: help with my php function for determining a person age

Posted: Sat Dec 10, 2011 12:54 am
by kshitizgp
u need to refer ur birthdate..

$birthdate = setdate(1990,5,15); or $birthdate ->setdate ()

$age = $today - $birthdate;
// Calculate total hours
$hours = round($difference / 60 / 60);


try doing it now

Re: help with my php function for determining a person age

Posted: Sat Dec 10, 2011 7:03 am
by Celauran
andreea115 wrote:i am a bit confused about this function. i.e what dates do i insert into the function. do i insert a unix timestamp or the the full date of birth. i tried inserting the bithdate but the fuction did not work.
DateTime
Either will work.