# Month & Year Countdown (Date Time Comparison)

Dear all Experts,

I can compare the hour by using the following line,
\$hourDiff = (mktime(0,0,0,\$month,\$day,\$year) - time())/3600;

in order to get day different,
\$dayDiff = (mktime(0,0,0,\$month,\$day,\$year) - time())/86400;

BUT,
How do i get Month and Year Different? For example i need to know how many months from 01-01-2003 to 31-05-2003?
\$monthDiff = (mktime(0,0,0,\$month,\$day,\$year) - time())/2678400; <---- This will only works for month that having 31 days (60sec*60mins*24hour*31days), what about this, (int)\$endMonth - \$startMonth?

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
\$endMonth = date("n",time());
\$startMonth = (int)\$month;

\$monthDiff = \$endMonth - \$startMonth

Commented:
hi,

for years you could just do:

\$endyear = date("Y", time());
\$startyear = \$year;
if(\$endyear > \$startyear) {
\$difference = \$endyear - \$startyear;
}

where both are 4 digit numbers

for months:

\$endmultiple = (\$endyear - 1970) * 12;
\$startmultiple = (\$startyear - 1970) * 12;
\$endmonth = date("n", time()) + \$endmultiple;
\$startmonth = \$month * (\$year + 1) + startmultiple;
\$monthdifference = \$endmonth - \$startmonth;

cheers,

loz

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:

\$yearNow           = date("Y",time());
\$yearCreated = (int)\$year;
\$yearDiff           = \$yearNow - \$yearCreated;

if (\$yearDiff > 0) {
\$xtraMonth = \$yearDiff * 12;
}

\$monthNow      = date("n",time());
\$monthCreated = (int)\$month;
\$monthDiff       = (\$monthNow - \$monthCreated) + \$xtraMonth;
Commented:
hi,

i don't get what this int thing is at the front of month and year?

yes that code should work (better than mine)

loz
Author Commented:
i'm casting the value into integer type

ref --> http://www.phpnoise.com/tutorials/22/5
Commented:
ok, i don't think they're particularly necessary but there's no real downside to them - have you tested the code? did it work?

loz
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.