date/time calculations: calculating exact difference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hans Gruber

    #1

    date/time calculations: calculating exact difference

    Hi all,

    I have been struggling with a problem all day, I have been unable to come
    up with a working solution.

    I want to write a function which takes 2 unix timestamps and calculates
    the difference. I want it to return the difference in years, months, days,
    hours, minutes and seconds (a complete summary). Keeping into account of
    course that these are 2 real dates, I dont want it to work with 30.475 as
    an average number of days in a month, I want it to work with the supplied
    months/years.

    The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
    56 minutes and 51 seconds difference.
    (I dont want the difference as in 1237438424 seconds but a complete
    summary).

    Any of my attempts have failed. It is easy to calculate the numbers of
    seconds, minutes, hours and days (total only). But I cant get passed
    calculating the number of months and years. Furthermore, any of the
    libraries from the internet which I have tested seem to be flawed as well,
    their results are not precise.

    Can this not be done? Anyone?

    Kind regards,

    Hans Gruber
  • Mike Willbanks

    #2
    Re: date/time calculations: calculating exact difference

    Hans Gruber wrote:[color=blue]
    > Hi all,
    >
    > I have been struggling with a problem all day, I have been unable to come
    > up with a working solution.
    >
    > I want to write a function which takes 2 unix timestamps and calculates
    > the difference. I want it to return the difference in years, months, days,
    > hours, minutes and seconds (a complete summary). Keeping into account of
    > course that these are 2 real dates, I dont want it to work with 30.475 as
    > an average number of days in a month, I want it to work with the supplied
    > months/years.
    >
    > The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
    > 56 minutes and 51 seconds difference.
    > (I dont want the difference as in 1237438424 seconds but a complete
    > summary).[/color]

    Look into the date functions. There are functions in there to tell you
    how many days are in the current month. Even if you wanted to do this
    by hand you could create an array with each month and how many days it
    has and check for a leap year.

    Then use the date function to work with these.... It will work, but to
    get it in your exact format might be a bit of work.

    Mike

    Comment

    • Hans Gruber

      #3
      Re: date/time calculations: calculating exact difference

      On Sun, 15 May 2005 20:58:54 -0500, Mike Willbanks wrote:
      [color=blue]
      > Hans Gruber wrote:[color=green]
      >> Hi all,
      >>
      >> I have been struggling with a problem all day, I have been unable to come
      >> up with a working solution.
      >>
      >> I want to write a function which takes 2 unix timestamps and calculates
      >> the difference. I want it to return the difference in years, months, days,
      >> hours, minutes and seconds (a complete summary). Keeping into account of
      >> course that these are 2 real dates, I dont want it to work with 30.475 as
      >> an average number of days in a month, I want it to work with the supplied
      >> months/years.
      >>
      >> The output ought to be: there is 0 years, 7 months, 12 days, 5 hours,
      >> 56 minutes and 51 seconds difference.
      >> (I dont want the difference as in 1237438424 seconds but a complete
      >> summary).[/color]
      >
      > Look into the date functions. There are functions in there to tell you
      > how many days are in the current month. Even if you wanted to do this
      > by hand you could create an array with each month and how many days it
      > has and check for a leap year.
      >
      > Then use the date function to work with these.... It will work, but to
      > get it in your exact format might be a bit of work.
      >
      > Mike[/color]

      Thanks Mike, I was actually thinking up something like that right before I
      went to bed last night. Another day of hard work coming up ;)

      Hans

      Comment

      • Geoff Berrow

        #4
        Re: date/time calculations: calculating exact difference

        I noticed that Message-ID: <pan.2005.05.16 .06.00.19.90600 0@nakatomi.de>
        from Hans Gruber contained the following:
        [color=blue]
        >Thanks Mike, I was actually thinking up something like that right before I
        >went to bed last night. Another day of hard work coming up ;)[/color]

        It's easy as you say, to convert seconds into minutes hours days and
        weeks. But if you said months what would you mean? Often the secret to
        coding something is the correct identification of the problem.

        Let's say I want to work out how many months it is till my next birthday
        (March 5th). What I'd probably do is ignore the month length difference
        and count up. It's May 16th today and so it's 9 months till February
        16th. Then it's 13 days till my birthday.

        If that's the way you want to work it out then you have your logic. I'm
        not going to code it but I think I'd be looking carefully at mktime() if
        I was.

        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • David Mackenzie

          #5
          Re: date/time calculations: calculating exact difference

          On Mon, 16 May 2005 07:54:48 +0100, Geoff Berrow
          <blthecat@ckdog .co.uk> wrote:
          [color=blue]
          >I noticed that Message-ID: <pan.2005.05.16 .06.00.19.90600 0@nakatomi.de>
          >from Hans Gruber contained the following:
          >[color=green]
          >>Thanks Mike, I was actually thinking up something like that right before I
          >>went to bed last night. Another day of hard work coming up ;)[/color]
          >
          >It's easy as you say, to convert seconds into minutes hours days and
          >weeks. But if you said months what would you mean? Often the secret to
          >coding something is the correct identification of the problem.
          >
          >Let's say I want to work out how many months it is till my next birthday
          >(March 5th). What I'd probably do is ignore the month length difference
          >and count up. It's May 16th today and so it's 9 months till February
          >16th. Then it's 13 days till my birthday.[/color]

          If your birthday was 30 June and today was 31 May would that be 1
          month or just 30 days until your birthday?

          Although if you decided the answer was "1 month", be aware you'd get
          the same answer on 30 May.

          There isn't really a "correct" answer, but the OP should consider
          these edge cases.

          However, it depends what data represents and how the user will use it.
          If long differences are shown to a lower resolution, this anomaly may
          not matter. If the time difference is several months, the user may not
          be interested in the value down to the minute and second.

          For example:

          DIFFERENCE :- WHAT TO SHOW[color=blue]
          > 60 days :- months, days.[/color]
          < 60 days :- days only.
          < 7 days :- days, hours.
          < 1 day :- hours, minutes.
          < 6 hours :- hours, minutes, seconds.

          --
          David ( @priz.co.uk )

          Comment

          Working...