Re: date() problem

From: Date: Thu, 07 Jul 2005 16:33:54 +0000
Subject: Re: date() problem
Groups: php.general 
Request: Send a blank email to php-general+get-218298@lists.php.net to get a copy of this message
Hey Richard, Thanks, you've pulled my butt outa the fire again :-) Cheers, Ryan On 7/6/2005 10:59:36 PM, Richard Lynch (ceo@l-i-e.com) wrote: > On Wed, July 6, 2005 12:07 pm, Ryan A said: > > > I'm confused, this should give me the age as 17 instead of 16...but it > > does > > not...any ideas why? > > > > <?php print date("Y:m:d"); > > $age="1988-07-06"; > > > > $day1=strtotime($age); > > $day2 = strtotime(date("Y-m-d")); > > $dif_s = ($day2-$day1); > > $dif_d = ($dif_s/60/60/24); > > $age = floor(($dif_d/365.24)); > > > > echo $age; > > ?> > > 365.24 is an appoximation. > > Sooner or later, it's > gonna bit you in the butt. > > If you want somebody's age accurately, you're probably going to have to > do > it the hard way. > > Something like this might work: > > <?php > $DOB = "1988-07-06"; > $now = date('Y-m-d'); > list($by, $bm, $bd) = explode('-', $DOB); > list($ny, $nm, $nd) = explode('-', $now); > $age = $ny - $by; > if ($age){ > if ($bm < $nm){ > // do nothing, they were born before this month > // so subtracting the years is correct > } > elseif ($nm < $bm){ > $age--; //They were born in a later month, so not quite a year yet > } > else{ > //They were born this month. Count the days. > if ($bd < $nd){ > //Do nothing. They were born before this date. } elseif ($nd < $bd){ $age--; //They were born later this month, so not quite a year yet } else{ //It's their birthday! Go ahead and count it as a year, //unless you want to futz with the hour of their birth... } } } if (!$age){ //Do whatever you want with children less than 1 year old... //return it as "X months" or just call it "infant" or pretend they're 1 //or whatever. //Maybe even just use $age (which is 0) and call it done. } ?> -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#218298) next »