Adding Days to a Date with PHP

前端 未结 3 510
滥情空心
滥情空心 2020-12-22 07:59

I am trying to add a certain amount of days to a set date in PHP. However, all of the code I use is not working. Here is the code I am currently experiencing problems with:<

相关标签:
3条回答
  • 2020-12-22 08:49

    It must be like this:

    $NewDate = date('Y-m-d', strtotime("2013-12-01" . " +7 days"));
    echo $NewDate;                                   
    
    0 讨论(0)
  • 2020-12-22 08:54

    For the sake of completeness, here's how you do it with DateTime():

    $datetime = new DateTime("2013-12-01");
    $datetime->add(new DateInterval('P7D'));
    echo $datetime->format('Y-m-d');
    

    or

    $datetime = new DateTime("2013-12-01");
    $datetime->modify('+7 days');
    echo $datetime->format('Y-m-d');
    
    0 讨论(0)
  • 2020-12-22 09:01

    You can use date_add() function:

    $date = date_create('2013-12-01');
    date_add($date, date_interval_create_from_date_string('7 days'));
    echo date_format($date, 'Y-m-d');
    

    This will output 2013-12-08

    0 讨论(0)
提交回复
热议问题