Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Month & Year Countdown (Date Time Comparison)

Posted on 2003-11-13
Medium Priority
321 Views
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?

0
Question by:paulsiew
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

Author Comment

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

\$monthDiff = \$endMonth - \$startMonth

0

LVL 13

Accepted Solution

lozloz earned 200 total points
ID: 9738032
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
0

Author Comment

ID: 9738095

\$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;
0

LVL 13

Expert Comment

ID: 9738277
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
0

Author Comment

ID: 9744434
i'm casting the value into integer type

ref --> http://www.phpnoise.com/tutorials/22/5
0

LVL 13

Expert Comment

ID: 9744827
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
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
###### Suggested Courses
Course of the Month11 days, 18 hours left to enroll