RE: Re: strtotime

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

 



> -----Original Message-----
> From: John Taylor-Johnston [mailto:John.Taylor-
> Johnston@xxxxxxxxxxxxxxxxxxxxx]
> Sent: Saturday, October 16, 2010 10:58 PM
> To: PHP-General
> Subject:  Re: strtotime
> 
> According to this, I'm 44 not 45 :)p
> 
> $birthday = '1965-08-30';
> 
> //calculate years of age (input string: YYYY-MM-DD)
> 
>    function birthday ($birthday){
>      list($year,$month,$day) = explode("-",$birthday);
>      $year_diff  = date("Y") - $year;
>      $month_diff = date("m") - $month;
>      $day_diff   = date("d") - $day;
>      if ($day_diff < 0 || $month_diff < 0)

I think the problem is with the logic above.

$year_diff = 44
$month_diff = 2
$day_diff = -13

Since the $month_diff is positive meaning you're already passed your
birthdate.  If the $month_diff is 0 - your birth month, then you need to
check if the if it's past your birth day yet (negative $day_diff).

That logic should be changed to if if (($month_diff == 0 && $day_diff < 0)
|| $month_diff < 0)

Then you're 45 ;)

Regards,
Tommy

>        $year_diff--;
>      return $year_diff;
>    }
> 
> echo birthday ($birthday);
> 
> 
> 
> John Taylor-Johnston wrote:
> > I'm working my way right now through the manual:
> > http://ca.php.net/manual/en/function.strtotime.php.
> >
> >


-- 
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