Re: how to find difference in months and years?

From: Date: Fri, 30 Jun 2000 23:24:35 +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-4202@lists.php.net to get a copy of this message
> > 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. It depends - a banker's month is just 30 days. However, you're probably going to want to use getdate() or date() to return the part of the date you care about. Most of this is application dependent - do you consider a year 365 days or when $Year changes? It's either $DiffInDays / 365 or date("Y", $LaterDate) - date("Y", $EarlierDate).

« previous php.general (#4202) next »