note 65044 deleted from function.date by derick

From: Date: Sun, 11 Jun 2006 11:46:14 +0000
Subject: note 65044 deleted from function.date by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-114265@lists.php.net to get a copy of this message
Note Submitter: mazaki at nospam dot shaw dot ca ---- I was writing a code to determine a users age from YYYY-MM-DD format and I think that this should work under all circumstances for any user. Although I've tested it under about 100 dates without any errors it still seems like it was too simple of an answer... <?php function get_age($birthdate) { // assumes $birthdate is in YYYY-MM-DD format list($dob_year, $dob_month, $dob_day) = explode('-', $birthdate); // determine current year, month, and day $cur_year = date('Y'); $cur_month = date('m'); $cur_day = date('d'); // either past or on the birthday if($cur_month >= $dob_month && $cur_day >= $dob_day) { $age = $cur_year - $dob_year; } // before the birthday else { $age = $cur_year - $dob_year - 1; } // and your done return $age." years old"; } ?> Usage: <?php // assuming todays date is 2006-04-24 echo get_age('1986-04-23'); // outputs 20 years old echo get_age('1986-04-24'); // outputs 20 years old echo get_age('1986-04-25'); // outputs 19 years old ?>

« previous php.notes (#114265) next »