Ads removed from the pastie. \o/
Running costs stayed. :-(
Donations welcome!

Bitcoin: 1QC38uPBSy7WSYz1whM5SYTHAdhqkN6o8V
Ethereum: 0x0855778a6181a69cc7d5d08c71c4f5846010eb67
Verge: D8fo1fszrNmUK4kfQW5owhYMv3osPvgn2g
Recent entries
2020-07-12 18:36:50
sdfsdfdsfsdfsdf
2020-07-12 18:31:33
Yellowstone 3x4 ~ New ser...
2020-07-12 18:31:08
ginkenyo
2020-07-12 18:30:45
itorioku
2020-07-12 18:30:25
rihiyozoku
2020-07-12 18:30:15
goyuzaoto
2020-07-12 18:27:05
https://www.residentadvis...
2020-07-12 18:24:56
tnesneseswtswt
2020-07-12 18:21:07
https://www.justgiving.co...
2020-07-12 18:17:12
https://www.residentadvis...
Permanent entries
2014-10-14 21:12:28
Boost property tree - Ite...
2013-10-18 08:24:21
Resize images in a folder...
2013-04-29 17:23:35
Find bad words in a text,...
2013-02-12 20:26:45
Merge two boost property ...
2012-12-18 16:21:40
OpenCV: Access/modify ima...
2012-11-19 10:48:40
Reading a boost::property...
2012-04-10 00:11:46
Convert hex to string or ...
2012-02-18 00:12:04
Screw up []-operator for ...
2012-02-16 03:02:24
Merge continuous numbers ...
2012-01-18 22:26:01
(PHP) Generate / Create (...
2011-04-19 16:05:25
Calculate one's age in ye...
2011-04-18 15:26:45
(PHP) Generate / Create r...
2010-08-26 15:18:00
Pick the first element (k...
2010-07-28 10:36:45
Quakenet - Snailbot comma...
2010-06-21 21:30:52
Using the MATLAB profiler...
2010-05-31 06:31:14
PHP truncation function
2010-05-06 20:49:26
Quakenet - Fishbot comman...
Paste ID: # dj9mK
Date posted: Tue, 19 Apr 2011 16:05:25 +0000
Date of expiration: never (permanent entry)
Description:

Calculate one's age in years from a given Date-of-Birth in PHP

  1. /**Return duration in years from DoB to today, i.e. someone's age in years.
  2.  * Date format is: yyyy-mm-dd
  3.  */
  4. function age( $date_of_birth )
  5. {
  6.   // Ensure $date_of_birth has correct format
  7.   if( !preg_match( '#\d{4}-\d{2}-\d{2}#', $date_of_birth ))
  8.     return -1; // ... or die() here or throw an exception or ... Whatever you want ... burning down the house
  9.  
  10.   $time = time(); // Note: This is the Server's time!
  11.   $dob  = explode( '-', $date_of_birth );
  12.   $now  = explode( '-', date( 'Y-m-d', $time ));
  13.  
  14.   $years = 0;
  15.   if( strtotime( $date_of_birth ) <= $time ) // actually ( $time + 1 year ) as the result would be 0 otherwise
  16.   {
  17.     // Difference in years
  18.     $years = $now[0] - $dob[0];
  19.  
  20.     /* The count of years may be incorrect for a birthday that is still to come this year
  21.      * so a small correction is required. First to check whether the birthday is in a
  22.      * month that is still left this year. If not check whether it is on a day to come
  23.      * this month. In either case reduce the years by 1. Done.
  24.      */
  25.     if(( $now[1] <  $dob[1] ) ||
  26.        ( $now[1] == $dob[1] ) && ( $now[2] < $dob[2] ))
  27.       $years--;
  28.   }
  29.  
  30.   return $years;
  31. }
You can paste a correction or amendment to the upper paste or a new paste down below.