calculating date diff and then adding it to a date

Discussion in 'PHP' started by dethfire, Sep 23, 2011.

  1. #1
    Let's say:
    $termdate = 9/10/11
    $addeddate = 8/11/11
    $nowdate = 9/23/11

    The result is calculated at 13 days diff and $newtermdate will be 09/23/11. That is fine.

    However let's say:
    $termdate = 12/09/11
    $addeddate = 8/11/11
    $nowdate = 9/23/11

    The result is still calculated at 13 days diff and $newtermdate will be 12/22/11. That is obviously wrong. Any ideas?


    
    $termdate = $row['termdate'];
    $addeddate = $row['date'];
    $nowdate = date("m/d/y");
    
    $diff = abs(strtotime($nowdate) - strtotime($addeddate));
    
    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    
    $newtermdate = strtotime('+'.$days.' day', strtotime($termdate));
    $newtermdate = date('m/d/y', $newtermdate);
    PHP:
     
    dethfire, Sep 23, 2011 IP
  2. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Try setting your dates as strings instead of pulling them from the database. See if it works as expected. If so, there may be a problem with the data you are pulling from the database.

    
    $termdate = '2011-09-10';
    $addeddate = '2011-08-11';
    $nowdate = date("m/d/y");
    
    $diff = abs(strtotime($nowdate) - strtotime($addeddate));
    
    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    
    $newtermdate = strtotime('+'.$days.' day', strtotime($termdate));
    $newtermdate = date('m/d/y', $newtermdate);
    echo $newtermdate;
    
    
    PHP:
     
    jevchance, Sep 23, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Why is 12/22/11 not 13 days later than 12/9/11? 9 + 13 = 22 the last time I looked. Did I miss something? (You're using m/d/y format.)
     
    Rukbat, Sep 25, 2011 IP