Add days to date() ???

Discussion in 'PHP' started by bobby9101, Nov 26, 2006.

  1. #1
    I use:
    $today = date("F - j - Y");

    however I want to add a certain number of days to it.
    Like: $nextweek = 7

    How can I add those 7 days to the current date? and still keep it in the same format.
     
    bobby9101, Nov 26, 2006 IP
  2. olaf2

    olaf2 Active Member

    Messages:
    1,362
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #2

    check the example from the manual:
    
    <?php
    $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),  date("Y"));
    $nextyear  = mktime(0, 0, 0, date("m"),  date("d"),  date("Y")+1);
    ?> 
    
    PHP:
     
    olaf2, Nov 26, 2006 IP
  3. kiwwi

    kiwwi Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Some functions I use with MySQL database :

    
    function dt_only_date_minus_x_days( $x ) {
      return date("Y-m-d", time( ) - ( $x * 24 * 60 * 60 ) );
    }
    
    function dt_minus_x_days( $x ) {
      return date("Y-m-d G:i:s", time( ) - ( $x * 24 * 60 * 60 ) );
    }
    
    function dt_now( ) {
      return dt_minus_x_days( 0 );
    }
    
    function dt_yesterday( ) {
      return dt_minus_x_days( 1 );
    }
    
    
    Code (markup):
    Hope It Helps.
     
    kiwwi, Nov 26, 2006 IP
  4. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #4
    @olaf2 that returns a timestamp, i need the same format
     
    bobby9101, Nov 26, 2006 IP
  5. olaf2

    olaf2 Active Member

    Messages:
    1,362
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    use the timestamp as the second argument of the date function (and maybe take a look in the manual) :D
     
    olaf2, Nov 26, 2006 IP
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this:

    
    $time = time() + (7 * 24 * 60 * 60);
    print $today = date("F - j - Y");              # today
    print $today = date("F - j - Y", $time);    # next week
    
    Code (markup):
     
    clancey, Nov 26, 2006 IP
    bobby9101 likes this.
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    
    
    date("F - j - Y", strtotime('+1 week'));
    
    PHP:
     
    nico_swd, Nov 26, 2006 IP
    bobby9101 likes this.
  8. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hey nico_swd, how can I make the +1 a variable so +weeks ? thanks
     
    bobby9101, Nov 26, 2006 IP
  9. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #9
    nvm, I used clacey's

    thanks very much... rep added to those that helped
     
    bobby9101, Nov 26, 2006 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Like this?

    
    $diff= strtotime('+1 week');
    
    $today = date("F - j - Y");
    $nextweek = date("F - j - Y", $diff);
    
    PHP:
     
    nico_swd, Nov 26, 2006 IP
  11. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #11
    Use strtotime()

    Great function :) You can figure out a timestamp with this little guy using english like words to do it

    $timestamp = strtotime("1 week ago");
    $timestamp = strtotime("5 hours later");
    $timestamp = strtotime("18 years ago");
    $timestamp = strtotime("30 days from now");
     
    drewbe121212, Nov 26, 2006 IP