Results 1 to 5 of 5

Thread: Difference Between Dates

  1. #1

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Difference Between Dates

    Does anyone know if there is a function that will tell me the amount of days between to dates?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Found this on PHP.net...


    T.E.
    07-Jan-2004 05:15
    Most functions calculating the difference in days between two days take some assumptions. They often ignore leap-years or decide to set a month to 30 days. This one here should work better and more precise.
    I left one thing to fix: I supposed a leap in every four years. This assumption is wrong when passing a xx00-border, if xx00 mod 400 != 0. However, PHPs date-function won't work with 1900 or less and 2100 or more. So for the next few years within the given boarders of PHP, this one should work.

    Code:
    function date_diff($dat1,$dat2)
    /* Dat1 and Dat2 passed as "YYYY-MM-DD" */
    {
      $tmp_dat1 = mktime(0,0,0,
         substr($dat1,5,2),substr($dat1,8,2),substr($dat1,0,4));
      $tmp_dat2 = mktime(0,0,0,
         substr($dat2,5,2),substr($dat2,8,2),substr($dat2,0,4));
    
      $yeardiff = date('Y',$tmp_dat1)-date('Y',$tmp_dat2);
      /* a leap year in every 4 years and the days-difference */
      $diff = date('z',$tmp_dat1)-date('z',$tmp_dat2) +
               floor($yeardiff /4)*1461;
    
      /* remainder */
      for ($yeardiff = $yeardiff % 4; $yeardiff>0; $yeardiff--)
       {
         $diff += 365 + date('L',
             mktime(0,0,0,1,1,
               intval(
                 substr(
                   (($tmp_dat1>$tmp_dat2) ? $dat1 : $dat2),0,4))
               -$yeardiff+1));
       }
    
      return $diff;
    }

  3. #3

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Thank You, Guess where this is going .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Electroman
    Thank You, Guess where this is going .

    Then I guess all downhill from now...

  5. #5

    Thread Starter
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by NoteMe
    Then I guess all downhill from now...



    (I got lost and forgot to reply to this )
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width