SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    difference of dates and then add days problem

    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?


    PHP Code:
    $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); 
    Free Science Homework Help
    http://www.physicsforums.com

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    Why are you not using the PHP function date_diff?
    PHP offers a great set of functions, already built, to manipulate dates and times.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess because I am using PHP 5.2 still
    Free Science Homework Help
    http://www.physicsforums.com

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    your question might be clearer to me if you gave an example of exact dates you have in your database, and exactly it is you want to derive from those 2 dates - a US type mm/dd/yyyy date? or a mysql ready date? And what is computation has to be done to get this date?

    Dont describe them - paste the dates here. Give 2 or more examples if you want.

  5. #5
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    your question might be clearer to me if you gave an example of exact dates you have in your database, and exactly it is you want to derive from those 2 dates - a US type mm/dd/yyyy date? or a mysql ready date? And what is computation has to be done to get this date?

    Dont describe them - paste the dates here. Give 2 or more examples if you want.
    I'm sorry for not being clear. Those are actual examples of dates in my database.

    I'm trying to find the difference in days between the $nowdate and $dateadded. The difference in days should be added to the $termdate.
    Free Science Homework Help
    http://www.physicsforums.com

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    $newdate = date('d/m/Y',strtotime($termdate) + abs(strtotime($nowdate) - strtotime($addeddate)));

    Dont bother doing the conversions. Slap everything into seconds, do the math, and then translate THAT.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Wizard dethfire's Avatar
    Join Date
    Aug 2000
    Posts
    2,477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made it so difficult on myself! Thanks StarLion!
    Free Science Homework Help
    http://www.physicsforums.com

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Even faster, since this is coming from the database use MySQL's date functions.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    From what the OP has shown I understand the OP is storing dates as mm/dd/yyyy and not yyyy-mm-dd, and that is one thing which is confusing me.

    Never mind, PO has got a sensible reply and is happy.


Bookmarks

Posting Permissions

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