Calculating Date Differences Help Needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Freshour

    #1

    Calculating Date Differences Help Needed

    What's a good way to calculate the number of days between two dates in
    the following format:

    2003-07-15
    2003-08-02

    I've looked at the PHP date functions but I'm still a bit lost...

  • Ralph Freshour

    #2
    Re: Calculating Date Differences Help Needed

    Yeah, I follow that - but I end up with a unix epoch date - how can I
    determine how many days diff that represents? In other words, per
    your suggestion I can get date1 and date2 and determine via mktime the
    diff - but I still need to know how many days diff that is???



    On Sun, 03 Aug 2003 03:18:26 GMT, Blaine HIlton
    <blaine.netmast err@verizon.net > wrote:
    [color=blue]
    >What you need to do is check the mktime() function at
    >http://www.php.net/manual/en/function.mktime.php.
    >
    >Basiclly you would use mktime and send the function the date and call
    >that date1 and then another mktime and call that date2. Then just
    >subtract. mktime() will give you the unix epoch date.
    >
    >
    >Also for a good example check out
    >http://www.befriend.com/code_gallery..._elapsed_time/, however
    >there are I believe 2 errors where a division sign should be a
    >multiplication .
    >
    >Mine looks like:
    >
    > //$divider['months'] = ( 60 * 60 * 24 * 365 / 12 );
    > $divider['months'] = ( 60 * 60 * 24 * 365 * 12 ); // cheanged
    >this one too
    > // $divider['weeks'] = ( 60 * 60 * 24 / 7 );
    > $divider['weeks'] = ( 60 * 60 * 24 * 7 ); // I changed / to *
    >
    >and can be seen in action at http://www.webcalc.net/calc/0529.php.[/color]

    Comment

    • Blaine HIlton

      #3
      Re: Calculating Date Differences Help Needed

      I would say first check out the 3 links that I gave:





      If you read through there you will have it working. Once you have the
      time from epoch for each date you subtract. That gives you the
      seconds, then times by 60 for minutes, then 60 more for hours then 24
      for days.

      --
      Blaine Hilton



      On Sun, 03 Aug 2003 06:00:32 GMT, Ralph Freshour <ralph@primemai l.com>
      wrote:
      [color=blue]
      >Yeah, I follow that - but I end up with a unix epoch date - how can I
      >determine how many days diff that represents? In other words, per
      >your suggestion I can get date1 and date2 and determine via mktime the
      >diff - but I still need to know how many days diff that is???
      >
      >
      >
      >On Sun, 03 Aug 2003 03:18:26 GMT, Blaine HIlton
      ><blaine.netmas terr@verizon.ne t> wrote:
      >[color=green]
      >>What you need to do is check the mktime() function at
      >>http://www.php.net/manual/en/function.mktime.php.
      >>
      >>Basiclly you would use mktime and send the function the date and call
      >>that date1 and then another mktime and call that date2. Then just
      >>subtract. mktime() will give you the unix epoch date.
      >>
      >>
      >>Also for a good example check out
      >>http://www.befriend.com/code_gallery..._elapsed_time/, however
      >>there are I believe 2 errors where a division sign should be a
      >>multiplicatio n.
      >>
      >>Mine looks like:
      >>
      >> //$divider['months'] = ( 60 * 60 * 24 * 365 / 12 );
      >> $divider['months'] = ( 60 * 60 * 24 * 365 * 12 ); // cheanged
      >>this one too
      >> // $divider['weeks'] = ( 60 * 60 * 24 / 7 );
      >> $divider['weeks'] = ( 60 * 60 * 24 * 7 ); // I changed / to *
      >>
      >>and can be seen in action at http://www.webcalc.net/calc/0529.php.[/color][/color]

      Comment

      • Ralph Freshour

        #4
        Re: Calculating Date Differences Help Needed

        I did check out the links you gave me - I understand the epoch number
        now - but when I do the math to get the number of days difference I
        get a huge number:

        // get todays date
        $now = time();
        print "<br>now: $now";
        $then = mktime(0,0,0,8, 15,2003);
        print "<br>then: $then";
        $diff = $now - $then;
        print "<br>diff: $diff";
        $min = $diff * 60;
        print "<br>min: $min";
        $hours = $min * 60;
        print "<br>hours: $hours";
        $days = $hours * 24;
        print "<br>days: $days";

        $days is a huge number and there is not that many days diff between
        time() and august 15, 2003 - what am I not understanding here?


        On Sun, 03 Aug 2003 07:23:52 GMT, Blaine HIlton
        <blaine.netmast err@verizon.net > wrote:
        [color=blue]
        >I would say first check out the 3 links that I gave:
        >
        >http://www.php.net/manual/en/function.mktime.php
        >http://www.befriend.com/code_gallery..._elapsed_time/
        >http://www.webcalc.net/calc/0529.php
        >
        >If you read through there you will have it working. Once you have the
        >time from epoch for each date you subtract. That gives you the
        >seconds, then times by 60 for minutes, then 60 more for hours then 24
        >for days.[/color]

        Comment

        • Joshua Ghiloni

          #5
          Re: Calculating Date Differences Help Needed

          Ralph Freshour wrote:[color=blue]
          > I did check out the links you gave me - I understand the epoch number
          > now - but when I do the math to get the number of days difference I
          > get a huge number:
          >
          > // get todays date
          > $now = time();
          > print "<br>now: $now";
          > $then = mktime(0,0,0,8, 15,2003);
          > print "<br>then: $then";
          > $diff = $now - $then;
          > print "<br>diff: $diff";
          > $min = $diff * 60;
          > print "<br>min: $min";
          > $hours = $min * 60;
          > print "<br>hours: $hours";
          > $days = $hours * 24;
          > print "<br>days: $days";
          >
          > $days is a huge number and there is not that many days diff between
          > time() and august 15, 2003 - what am I not understanding here?
          >[/color]
          What you're not understanding is the fact that his math was wrong ;) If
          you've got 60 seconds in a minute, then there are 60 / 60 = 1 minute in
          a minute, not 60 * 60 = 3600 minutes in a minute. Change all those * to
          / and it should work.

          Comment

          • Ralph Freshour

            #6
            Re: Calculating Date Differences Help Needed

            I got it - I finally got it to work...

            Thanks...

            On Sun, 03 Aug 2003 07:23:52 GMT, Blaine HIlton
            <blaine.netmast err@verizon.net > wrote:
            [color=blue]
            >I would say first check out the 3 links that I gave:
            >
            >http://www.php.net/manual/en/function.mktime.php
            >http://www.befriend.com/code_gallery..._elapsed_time/
            >http://www.webcalc.net/calc/0529.php
            >
            >If you read through there you will have it working. Once you have the
            >time from epoch for each date you subtract. That gives you the
            >seconds, then times by 60 for minutes, then 60 more for hours then 24
            >for days.[/color]

            Comment

            • Blaine HIlton

              #7
              Re: Calculating Date Differences Help Needed

              Yes sorry about that / is a big difference then ;-)

              --
              Blaine

              On Sun, 03 Aug 2003 10:35:48 -0400, Joshua Ghiloni
              <jdg11@SPAM.ME. AND.DIE.cwru.ed u> wrote:
              [color=blue]
              >Ralph Freshour wrote:[color=green]
              >> I did check out the links you gave me - I understand the epoch number
              >> now - but when I do the math to get the number of days difference I
              >> get a huge number:
              >>
              >> // get todays date
              >> $now = time();
              >> print "<br>now: $now";
              >> $then = mktime(0,0,0,8, 15,2003);
              >> print "<br>then: $then";
              >> $diff = $now - $then;
              >> print "<br>diff: $diff";
              >> $min = $diff * 60;
              >> print "<br>min: $min";
              >> $hours = $min * 60;
              >> print "<br>hours: $hours";
              >> $days = $hours * 24;
              >> print "<br>days: $days";
              >>
              >> $days is a huge number and there is not that many days diff between
              >> time() and august 15, 2003 - what am I not understanding here?
              >>[/color]
              >What you're not understanding is the fact that his math was wrong ;) If
              >you've got 60 seconds in a minute, then there are 60 / 60 = 1 minute in
              >a minute, not 60 * 60 = 3600 minutes in a minute. Change all those * to
              >/ and it should work.[/color]

              Comment

              Working...