note 65044 added to function.date

From: Date: Mon, 24 Apr 2006 12:56:35 +0000
Subject: note 65044 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-109424@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 66.207.199.35 Probable Submitter: 24.67.135.122 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/note/edit/65044 Del: integrated -- http://master.php.net/note/delete/65044/integrated Del: useless -- http://master.php.net/note/delete/65044/useless Del: bad code -- http://master.php.net/note/delete/65044/bad+code Del: spam -- http://master.php.net/note/delete/65044/spam Del: non-english -- http://master.php.net/note/delete/65044/non-english Del: in docs -- http://master.php.net/note/delete/65044/in+docs Del: other reasons-- http://master.php.net/note/delete/65044 Reject -- http://master.php.net/note/reject/65044 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109424) next »