Increase current date by 5 days

后端 未结 11 1923
执笔经年
执笔经年 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: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
    );
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题