Re: date() problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx) 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux