Date difference in days....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Dean

    #1

    Date difference in days....

    Probably being a little thick here, but when you subtract one date away
    from another, how do you convert the resultant value into a number of
    days... I guess I could easily / 60 / 60 / 24... but that seems
    barbaric... Anything neater?

    Cheers
    Simon

    Ps, Im also just trying to work out how to calculate the number of
    mondays and tuesdays etc between two dates... Just thought I'd run this
    theory past you if I may to gain more confidence.

    I figure getting the date difference, and knowing that the starting date
    falls on a Thursday holds the key... working out the days between these
    two dates should be easy enough to divide by 7 to get the whole number
    and remainder (is that a mod?)... we can just say there's x number of
    Wednesdays, x number of Thursdays etc, and then just loop through the
    remainder which is a small number so not much of system load?

    then we can work out how many of those days were working days, subtract
    holiday dates from each count... and... well, that's my thoughts so far...

    Workable?
  • Mara Guida

    #2
    Re: Date difference in days....

    Simon Dean wrote:[color=blue]
    > ... subtract one date away from another
    > ... convert [...] into a number of days
    > ... calculate the number of mondays and tuesdays etc between two dates[/color]
    <snip>[color=blue]
    > Workable?[/color]


    Check these out:




    I got the links from the php.net manual page for date().
    Other than checking they're not dead links I haven't tested them.
    Right now it's the second 'User Contributed Note' from "aidan at php
    dot net".

    Comment

    • Gordon Burditt

      #3
      Re: Date difference in days....

      >Probably being a little thick here, but when you subtract one date away[color=blue]
      >from another, how do you convert the resultant value into a number of
      >days... I guess I could easily / 60 / 60 / 24... but that seems
      >barbaric... Anything neater?[/color]

      First off, you need to nail down what answer you want a little
      more. You will not always get agreement on these:

      1. How many days are there between January 1 and January 2?
      My answer: NONE, those two days are next to each other, and
      therefore none of them are Mondays regardless of what year it is.

      2. How many days are there between January 2 and January 2 (of
      the same year)? My answer: this doesn't really make sense, but
      if you insist, negative one.

      If, on the other hand, you're trying to figure out how many work
      days there are between the morning of date X and the evening of date Y,
      some people might consider the answers to the above questions as
      2 and 1, respectively.
      [color=blue]
      >Ps, Im also just trying to work out how to calculate the number of
      >mondays and tuesdays etc between two dates... Just thought I'd run this
      >theory past you if I may to gain more confidence.
      >
      >I figure getting the date difference, and knowing that the starting date
      >falls on a Thursday holds the key... working out the days between these
      >two dates should be easy enough to divide by 7 to get the whole number
      >and remainder (is that a mod?)... we can just say there's x number of
      >Wednesdays, x number of Thursdays etc, and then just loop through the
      >remainder which is a small number so not much of system load?[/color]

      I had occasion to do some calculations to determine the next <day
      of week> or first <day of week> in <month> after the given date.
      Your calculations will likely involve a lot of % 7 or / 7 operations,
      occasionally with adding 7 to avoid negative numbers being fed to
      %. You can probably avoid a loop.

      Gordon L. Burditt

      Comment

      • Simon Dean

        #4
        Re: Date difference in days....

        Gordon Burditt wrote:[color=blue][color=green]
        >> Probably being a little thick here, but when you subtract one date
        >> away[/color]
        >[color=green]
        >> from another, how do you convert the resultant value into a number
        >> of[/color]
        >[color=green]
        >> days... I guess I could easily / 60 / 60 / 24... but that seems
        >> barbaric... Anything neater?[/color]
        >
        >
        > First off, you need to nail down what answer you want a little more.
        > You will not always get agreement on these:
        >
        > 1. How many days are there between January 1 and January 2? My
        > answer: NONE, those two days are next to each other, and therefore
        > none of them are Mondays regardless of what year it is.[/color]

        Inclusive of the start day and end day.

        [color=blue]
        > 2. How many days are there between January 2 and January 2 (of the
        > same year)? My answer: this doesn't really make sense, but if you
        > insist, negative one.[/color]

        1.

        [color=blue]
        > If, on the other hand, you're trying to figure out how many work days
        > there are between the morning of date X and the evening of date Y,
        > some people might consider the answers to the above questions as 2
        > and 1, respectively.[/color]

        I figure somehow, will need to somewhat ignore the time, maybe get the
        two dates, set the time to the same value, then work out the day
        difference and add one to get the result I want...

        Still though, don't know how to convert the result of $date1 - $date2 to
        something more useful, like, minutes, hours, or days.... That's the only
        stumbling block as far as Im concerned, Im sure I can work out the other
        bits.

        Cheers
        Simon

        Comment

        • C.

          #5
          Re: Date difference in days....

          The Pear package is not exactly well documented.

          These all seem to rely on Unix timestamps - which are only cover a
          relatively small interval (32 bits 1970 - 2038 IIRC).

          Since most the stuff I write is talking to a DBMS anyway I usually use
          the MySQL date manipulation functions. These include a datediff()
          function.

          HTH

          C.

          Comment

          • Ken Robinson

            #6
            Re: Date difference in days....

            Simon Dean wrote (in part):[color=blue]
            > Probably being a little thick here, but when you subtract one date away
            > from another, how do you convert the resultant value into a number of
            > days... I guess I could easily / 60 / 60 / 24... but that seems
            > barbaric... Anything neater?[/color]

            This the simpliest function I could come up with for getting the
            difference between two dates. Note that I didn't code any error
            checking.

            <?php
            echo date_diff ("20051210", "20051230") ;

            function date_diff($s,$e )
            {
            return((strtoti me($e) - strtotime($s))/86400);
            }
            ?>

            Since the strtotime() function returns the number of seconds since
            1/1/1970, you need to divide the result by the number of seconds in a
            day (86400) to get the number of days.

            Ken

            Comment

            Working...