RE: [PHP] Re: strtotime
| From: | Tommy Pham | Date: | Sun, 17 Oct 2010 14:01:19 +0000 |
| Subject: | RE: [PHP] Re: strtotime | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308765@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: John Taylor-Johnston [mailto:John.Taylor-
> Johnston@cegepsherbrooke.qc.ca]
> Sent: Saturday, October 16, 2010 10:58 PM
> To: PHP-General
> Subject: [PHP] 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.
> >
> >