Results 1 to 9 of 9

Thread: difference between 2 date

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    difference between 2 date

    Is there any function to give me the difference between 2 date

    for exam:

    dif beteen 05-24-2002 and 05-27-2002 = 3 days
    Last edited by prokhaled; Jun 18th, 2002 at 10:31 AM.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Just use the Date() and strtotime() functions:

    PHP Code:
    echo date("d M Y"strtotime("05-28-2002")); 
    the Date() function formats the date, and the strtotime() function converts the text date into a unix timestamp.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    http://www.php.net/manual/en/function.date.php contains information on the function and many format strings that you might want to know.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You changed your question...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Yes

    I'm Sorry.

    do you have answer for my new question ??

  6. #6
    scoutt
    Guest
    if you keep the date like it is and then next year you will have problems. you need to make the date like this 2002-5-24 as you can do comparisopns on dates a lot easier. the reason is that you have a date 5-24-2003 - 5-24-2002 is not 365 as far as php is concerned. but 2003-5-24 - 2002-5-24 will be don't ask me how but I have come across this.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This should give you the difference in days:

    PHP Code:
    <?php
     
      $date1 
    "05-24-2002";
      
    $date2 "05-27-2002";
      
      echo 
    DateDiff($date1$date2);

      function 
    DateDiff($first$second) { 
        
    $t1 strtotime($first); 
        
    $t2 strtotime($first); 
        
    $dif $t1 $t2
        
    $days floor(($dif / (60*60*24))); 
        
        return 
    $days


    ?>
    You could probably expand it farther to get weeks and years (months are too complicated and I don't feel like working it out (since each month has a different number of days)):

    PHP Code:
    <?php

      
    function DateDiff($first$second) { 
        
    $t1 strtotime($first); 
        
    $t2 strtotime($first); 
        
    $dif $t1 $t2
        
    $days floor(($dif / (60*60*24)));
       
        if (
    $days 365) { //not leap year compliant
          
    $years floor($days 365) . " years";
          
    $days floor($days 365);
        if (
    $days 7) {
          
    $weeks floor($days 7) . " weeks";
          
    $days floor($days 7) . " days";
        } 
      
        if (
    $years != 0) {
          
    $diff $years;
        }
        if (
    $weeks != 0) {
          
    $diff .= ", $weeks";
        }
        if (
    $days !=0) {
          
    $diff .= ", $days";
        }
        
    $diff .= ".";

        return 
    $diff


    ?>
    Something like this.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    V.G The Hobo

    It's very good idea.
    Thanks

  9. #9
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    My evil laugh has a squeak in it.

    kristopherwilson.com

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