| | |
Calculate AGE using PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi, I want to calculate age of the person using this code
but it gives error
php Syntax (Toggle Plain Text)
<?php /*** a date before 1970 ***/ $dob = strtotime("april 20 1961"); /*** another date ***/ $tdate = strtotime("june 2 2009"); /*** show the date ***/ echo getAge( $dob, $tdate ); ?>
but it gives error
Fatal error: Call to undefined function getage() Ayesha
this code working perfectly.
php Syntax (Toggle Plain Text)
<?php function yearsOld($birthday) { if (($birthday = strtotime($birthday)) === false) { return false; } for ($i = 0; strtotime("-$i year") > $birthday; ++$i); return $i - 1; } /*** example usage ***/ $birthday = 'april 20 1961'; echo yearsOld($birthday).' years old'; ?>
Ayesha
It is probably faster to do something like:
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
Then subtracts the birthday from the current unix time. This gives you the seconds elapsed between their birthday and today.
Then retrieves the number years that would have elapsed in those seconds, since year 1970.
Then subtracts 1970 from that, to get the number years since birth (year 0).
Edit: limited to dates php can calculate with unix time.
php Syntax (Toggle Plain Text)
function age($birthday) { return date("Y", time() - strtotime($birthday)) - 1970; }
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)
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)
time() - strtotime($birthday)
Then retrieves the number years that would have elapsed in those seconds, since year 1970.
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- calculate age (VB.NET)
- age caluculation in php... (PHP)
- New to C program code to calculate age (C)
- Calculating age (PHP)
- Calculate Age in VB 2005 (VB.NET)
- age calculator (Computer Science)
- Age Calculator (C)
Other Threads in the PHP Forum
- Previous Thread: displaying php results in a table
- Next Thread: Could not start the MySQL service
Views: 570 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code codingproblem cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select send server sessions sms soap source space speed sql static structure syntax system table tutorial up-to-date update updates upload url validation validator variable video web wordpress xml youtube






