Re: how to find difference in months and years?
| From: | Matt McClanahan | Date: | Sat, 01 Jul 2000 20:45:09 +0000 |
| Subject: | Re: how to find difference in months and years? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-4294@lists.php.net to get a copy of this message | ||
On Fri, 30 Jun 2000, Daevid Vincent wrote:
> > Actually, it has a really powerful date system.
> > date()/time()/mktime() are arguably more powerful
> > than anything MySQL has and considerably faster. Use mktime() to
> > turn a textual date into a Unix
> > timestamp, which is simply stored as seconds since an epoch:
> >
> > 1 day = 24 * 60 * 60 seconds = 86400
> >
> > To find the number of days between two dates, just subtract one
> > from the other and divide by 86400:
> >
> > $DiffInSeconds = (mktime(0,0,0, 10, 1, 2000) - mktime(0,0,0,
> > 06, 1, 2000))
> >
> > $DiffInDays = $DiffInSeconds / 86400
>
> So, this tells me the difference in days, but how can I tell how many months
> or years between two dates, since they'll have a different number of days in
> each month? I can't just divide the $DiffInDays by 30 because some months
> have 28,29,30, or 31 days in them.
Well, if you have two YYYY-MM-DD's, assuming the earlier of the two is
$year1 $month1 $day1,..
if( $year1 < $year2 )
{
$monthdif = 12 - $month1;
$monthdif += $month2;
}
else if( $year1 == $year2 )
{
$monthdif = $month2 - $month1;
}
else
{
// The first date didn't come before the last date.
}
That should take care of the month differences.
Matt