Increase current date by 5 days

后端 未结 11 1921
执笔经年
执笔经年 2021-01-02 16:57
$date = date(\'Y-m-d\',current_time(\'timestamp\', 0));

How do I change $date to $date + 5 days?

PHP version is 5

相关标签:
11条回答
  • 2021-01-02 17:16

    Did not supposed to be like this?

    $date_cur = date('Y-m-d', current_time('timestamp', 0));
    echo $date_cur . ' <br>';
    $date_cur_plus = date('Y-m-d', strtotime('+5 days', current_time('timestamp', 0) ) );
    echo $date_cur_plus;
    
    0 讨论(0)
  • 2021-01-02 17:20

    I used this:

    $date = strtotime("+1 day", strtotime("2007-02-28"));
    echo date("Y-m-d", $date);
    

    It's working now.

    0 讨论(0)
  • 2021-01-02 17:21

    You could use mktime() using the timestamp.

    Something like:

    $date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y')));
    

    Using strtotime() is faster, but my method still works and is flexible in the event that you need to make lots of modifications. Plus, strtotime() can't handle ambiguous dates.

    Edit

    If you have to add 5 days to an already existing date string in the format YYYY-MM-DD, then you could split it into an array and use those parts with mktime().

    $parts = explode('-', $date);
    $datePlusFive = date(
        'Y-m-d', 
        mktime(0, 0, 0, $parts[1], $parts[2] + 5, $parts[0])
        //              ^ Month    ^ Day + 5      ^ Year
    );
    
    0 讨论(0)
  • 2021-01-02 17:22

    For specific date:

    $date = '2011-11-01';
    $date_plus = date('Y-m-d', strtotime($date.'+5 days'));
    echo $date.'<br>'.$date_plus;
    

    It will be give :

    2011-11-01
    2011-11-06
    
    0 讨论(0)
  • 2021-01-02 17:23

    Object oriented Style:

    <?php
        $date = new DateTime('now');
        $date->add(new DateInterval('P5D'));
        echo $date->format('Y-m-d') . "\n";
    ?>
    

    Procedural Style:

    <?php
        $date = date_create('2016-01-01');
        date_add($date, date_interval_create_from_date_string('5 days'));
        echo date_format($date, 'Y-m-d');
    ?>
    
    0 讨论(0)
  • 2021-01-02 17:23
    $dateplus5 = date('Y-m-d', strtotime('+5 days'));
    
    0 讨论(0)
提交回复
热议问题