943,806 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1840
  • PHP RSS
Jul 1st, 2009
0

Calculate AGE using PHP

Expand Post »
Hi, I want to calculate age of the person using this code
php Syntax (Toggle Plain Text)
  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()
Similar Threads
Reputation Points: 17
Solved Threads: 6
Posting Pro in Training
ayesha789 is offline Offline
485 posts
since Jun 2009
Jul 1st, 2009
0

Re: Calculate AGE using PHP

this code working perfectly.

php Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 17
Solved Threads: 6
Posting Pro in Training
ayesha789 is offline Offline
485 posts
since Jun 2009
Jul 1st, 2009
0

Re: Calculate AGE using PHP

It is probably faster to do something like:

php Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. strtotime($birthday)

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

PHP Syntax (Toggle Plain Text)
  1. time() - strtotime($birthday)

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

PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: displaying php results in a table
Next Thread in PHP Forum Timeline: Could not start the MySQL service





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC