Solved

Month & Year Countdown (Date Time Comparison)

Posted on 2003-11-13
6
297 Views
Last Modified: 2008-02-01
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?


Pls Advice... Thanks in Advance! :)



0
Comment
Question by:paulsiew
  • 3
  • 3
6 Comments
 

Author Comment

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

$monthDiff = $endMonth - $startMonth


how bout this? Pls Advice...
0
 
LVL 13

Accepted Solution

by:
lozloz earned 100 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

by:paulsiew
ID: 9738095
what about this?

                        $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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:lozloz
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

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

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

Expert Comment

by:lozloz
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question