Solved

Month & Year Countdown (Date Time Comparison)

Posted on 2003-11-13
6
268 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
Comment Utility
$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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Expert Comment

by:lozloz
Comment Utility
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
Comment Utility
i'm casting the value into integer type

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

Expert Comment

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now