difference between dates

Discussion in 'PHP' started by kharearch, Dec 16, 2007.

  1. #1
    I want to find difference between two dates. For this how can I change date format and subtract two dates.
     
    kharearch, Dec 16, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Can you post some exact examples of what you want to do?
     
    nico_swd, Dec 16, 2007 IP
  3. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    One way is to store the dates using the time() function, then you can the calculate the difference between the two dates as seconds. So if you had a difference between the two figures of 86400 that would be one day, 864000 would be ten days and so on.
     
    Gawk, Dec 16, 2007 IP
  4. kharearch

    kharearch Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    I am subtracting exp_date from today's date. Exp_date is coming from table which is in the format yyyy-mm-dd. How can I change my today's date format to yyyy-mm-dd. So that I can subtract today's date from expiry date.I want difference in days. Please answer the following questions-

    i) what is the syntex of datediff?
    ii) which function I should use to find today's date?
    iii) How to change date format
     
    kharearch, Dec 16, 2007 IP
  5. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
  6. WeBuster

    WeBuster Member

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    2. Check Date() function.

    3. Check explode() function,
    But I think you should google "php date format"...
    I'm sure there is something better than using explode().
     
    WeBuster, Dec 16, 2007 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
  8. sunnyverma1984

    sunnyverma1984 Well-Known Member

    Messages:
    342
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    120
    #8
    function dateDiff($dformat, $endDate, $beginDate)
    {
    $date_parts1=explode($dformat, $beginDate);
    $date_parts2=explode($dformat, $endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;
    }
    PHP:
    Now let us see how we use this function:

    $date1="07/11/2003";
    $date2="09/04/2004";

    print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";

    which generates If we minus 07/11/2003 from 09/04/2004 we get 421.
     
    sunnyverma1984, Dec 17, 2007 IP