|
Posted by Toby A Inkster on 04/11/07 17:10
un1xg33k wrote:
> Is there a php function to determine the difference between dates.
Yes: it's pronounced "minus" and written "-".
> I'd like to take a birthdate and then determine how old a person is
> dynamically and simply return the age in years.
<?php
$birthdate = strtotime('1 June 1980');
$now = time();
$diff_in_seconds = $now - $birthdate;
$diff_in_days = $diff_in_seconds / (24*60*60);
$diff_in_years = $diff_in_days / 365.25;
printf("I am %d years old!", (int)$diff_in_years);
?>
The above may misbehave on the day before/after/of someone's birthday due
to stupid bloody leap years, which are always annoying, but it should give
you a basic idea of how date calculations can be done.
If you're very concerned about them, it's not rocket science to counteract
the effect of leap years.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there!
Navigation:
[Reply to this message]
|