How to get the date of 3 days later of today

Discussion in 'PHP' started by amenda, Sep 26, 2008.

  1. #1
    I know date("Y/m/d") is to get today's date like 2008/09/26, and now I want to know the date of 3 days later from date("Y/m/d"). How to do that?

    How to automatically increase date? For example, after getting date("Y/m/d"), now I want to show the date after it one by one.
     
    amenda, Sep 26, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    $three_days_later = strtotime("+3 days"); //in timestamp
    
    // get it in date form
    $newdate = date("Y/m/d",$three_days_later);
    
    //print it out 
    print $newdate;
    PHP:
     
    ads2help, Sep 26, 2008 IP
  3. tradeout

    tradeout Peon

    Messages:
    92
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Think i'd make use of the mktime() function

    $threeDaysLater = date("Y/m/d", mktime(0, 0, 0, date("m") , date("d")+3, date("Y")));
     
    tradeout, Sep 26, 2008 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    mktime and strtotime can achieve want you want done
     
    serialCoder, Sep 26, 2008 IP
  5. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How to increase the date by one day continuously?
     
    amenda, Sep 26, 2008 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    in what way? perhaps use a loop.

    
    $num_loop = 40; // number of loops :: start from today, print 40 dates
    for ($looped=0;$looped<$num_loop;$looped++) {
    print date("Y/m/d" , strtotime("+$looped days") ); // print out the date
    }
    
    PHP:
    or you want to assign them to an array?
     
    ads2help, Sep 26, 2008 IP
  7. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes, I want to assign it to an array.

    Another question, if I now $myDate = "2008/07/25", how to add 3 days to it and then show 2008/08/28?
     
    amenda, Sep 26, 2008 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    
    $num_loop = 9; // number of loops
    for ($looped=0;$looped<$num_loop;$looped++) {
    $date_array[$looped] = date("Y/m/d" , strtotime("+$looped days") );
    // the array of date start from today & run for $num_loop times
    }
    
    PHP:
    $date_array will be the array

    For your another question:
    you mean add a month also? it will be:
    $three_days_n_one_month_later = strtotime("+3 days + 1 month"); //timestamp
    
    // get it in date form
    $newdate = date("Y/m/d",$three_days_n_one_month_later);
    
    //print it
    print $newdate;
    
    PHP:
     
    ads2help, Sep 26, 2008 IP
  9. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No, I mean if I only know $myDate, how do I know 3 days later of $myDate? For example, if I knew $myDate = 2008/07/25, how to add 3 days to $myDate and make it show 2008/07/28.
     
    amenda, Sep 26, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    $num_loop = 9; // number of loops
    for ($looped=0;$looped<$num_loop;$looped++) {
    $date_array[$looped] = date("Y/m/d" , strtotime("+$looped days") );
    // the array of date start from today & run for $num_loop times
    }
    PHP:
    $date_array will be the array

    $date_array[3] will be 3 days later
     
    ads2help, Sep 26, 2008 IP
  11. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Since date("Y/m/d") will get the date of today, not a specified date (maybe a date in past or in future). For example I will record a date as $myDate, and I want to add 3 days to $myDate to get the date of 3 days later after $myDate. How to do that?
     
    amenda, Sep 26, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    just add the date into the strtotime() the like below:
    
    $date_later = strtotime("2008/10/21 +3 days + 1 month"); //in timestamp
    
    // get it in date form
    $newdate = date("Y/m/d",$date_later);
    
    PHP:
    or

    
    $originaldate = '2008/10/21';
    $date_later = strtotime("$originaldate +3 days + 1 month"); //in timestamp
    
    // get it in date form
    $newdate = date("Y/m/d",$date_later);
    
    PHP:
    and finally the array form you want:
    
    $num_loop = 9; // number of loops
    $originaldate = '2008/9/27';
    for ($looped=0;$looped<$num_loop;$looped++) {
    $date_array[$looped] = date("Y/m/d" , strtotime("$originaldate +$looped days") );
    }
    
    PHP:
    the array above is limited to num of loops
    this function below can do for any date (controllable future or past), maybe its a long step =.=
    
    function getthedate($now,$changein_day,$pom_day,$changein_month,$pom_month) {
    // $now in the form : 2008/8/17
    /* the script below doesn't work =.=  why?
    if ($changein_day >= 0) {$day = '+'.$changein_day;} elseif ($changein_day < 0) { $day = '-'.$changein_day; }
    if ($changein_month >= 0) {$month = '+'.$changein_month;} elseif ($changein_month < 0) { $month = '-'.$changein_month; }
    $changed_ts = strtotime("$now $day days $month months"); //in timestamp
    $newdate = date("Y/m/d",$changed_ts);
    */
    if ($pom_day == '-') { $pom_day = '-'; } else { $pom_day= '+';}
    if ($pom_month == '-') { $pom_month = '-'; } else { $pom_month= '+';}
    $changed_ts = strtotime("$now $pom_day $changein_day days $pom_month $changein_month months"); //in timestamp
    $newdate = date("Y/m/d",$changed_ts);
    return $newdate;
    }
    
    print getthedate('2008/5/5',10,'+',1,'-'); // + 10 days - 1 month
    // getthedate('2008/5/5',1,'-',10,'-'); // - 1 day - 10 month
    // getthedate('2008/5/5',5,'+',50,'+'); // + 5 day + 50 month
    
    PHP:
     
    ads2help, Sep 26, 2008 IP
  13. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Another way to do this is by knowing that strtotime() accepts a 2nd argument used as the offset timestamp. The default is of course time() (meaning now) so if you passed strtotime($myDate) as the 2nd argument then it would do the day increase to that one.

    strtotime("+3 days", strtotime($myDate)) would add 3 days to the $myDate timestamp. Just seems a bit cleaner to me than strtotime("$myDate +3 days") for some reason. Not sure of its efficiency though since it's calling that function twice.
     
    zerxer, Sep 26, 2008 IP
  14. amenda

    amenda Banned

    Messages:
    778
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks all, I tried $incDate=strtotime("$startDate + 3 days"); plus date("Y/m/d", $incDate); and it's working.
     
    amenda, Sep 26, 2008 IP
  15. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #15
    Thnx for sharing this tip bro :)
     
    ghprod, Sep 28, 2008 IP