Calculate AGE using PHP

Thread Solved

Join Date: Jun 2009
Posts: 225
Reputation: ayesha789 is an unknown quantity at this point 
Solved Threads: 3
ayesha789's Avatar
ayesha789 ayesha789 is offline Offline
Posting Whiz in Training

Calculate AGE using PHP

 
0
  #1
Jul 1st, 2009
Hi, I want to calculate age of the person using this code
  1. <?php
  2.  
  3. /*** a date before 1970 ***/
  4. $dob = strtotime("april 20 1961");
  5.  
  6. /*** another date ***/
  7. $tdate = strtotime("june 2 2009");
  8.  
  9. /*** show the date ***/
  10. echo getAge( $dob, $tdate );
  11.  
  12. ?>

but it gives error
Fatal error: Call to undefined function getage()
Ayesha
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 225
Reputation: ayesha789 is an unknown quantity at this point 
Solved Threads: 3
ayesha789's Avatar
ayesha789 ayesha789 is offline Offline
Posting Whiz in Training

Re: Calculate AGE using PHP

 
0
  #2
Jul 1st, 2009
this code working perfectly.

  1. <?php
  2.  
  3.  
  4. function yearsOld($birthday)
  5. {
  6. if (($birthday = strtotime($birthday)) === false)
  7. {
  8. return false;
  9. }
  10. for ($i = 0; strtotime("-$i year") > $birthday; ++$i);
  11. return $i - 1;
  12. }
  13.  
  14.  
  15.  
  16. /*** example usage ***/
  17. $birthday = 'april 20 1961';
  18.  
  19. echo yearsOld($birthday).' years old';
  20.  
  21. ?>
Ayesha
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,085
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Calculate AGE using PHP

 
0
  #3
Jul 1st, 2009
It is probably faster to do something like:

  1. function age($birthday) {
  2. return date("Y", time() - strtotime($birthday)) - 1970;
  3. }

It first converts the $birthday string to Unix Time. Which is the number of seconds since the epoch (midnight Jan 1 1970 UTC). http://en.wikipedia.org/wiki/Unix_time

  1. strtotime($birthday)

Then subtracts the birthday from the current unix time. This gives you the seconds elapsed between their birthday and today.

  1. time() - strtotime($birthday)

Then retrieves the number years that would have elapsed in those seconds, since year 1970.

  1. date("Y", time() - strtotime($birthday))

Then subtracts 1970 from that, to get the number years since birth (year 0).

Edit: limited to dates php can calculate with unix time.
Last edited by digital-ether; Jul 1st, 2009 at 7:50 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 570 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC