Increase days to php current Date()

前端 未结 11 1336
予麋鹿
予麋鹿 2020-12-04 19:13

How do I add a certain number of days to the current date in PHP?

I already got the current date with:

$today = date(\'y:m:d\');

Ju

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 19:52

    $NewTime = mktime(date('G'), date('i'), date('s'), date('n'), date('j') + $DaysToAdd, date('Y'));

    From mktime documentation:

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.

    The advantage of this method is that you can add or subtract any time interval (hours, minutes, seconds, days, months, or years) in an easy to read line of code.

    Beware there is a tradeoff in performance, as this code is about 2.5x slower than strtotime("+1 day") due to all the calls to the date() function. Consider re-using those values if you are in a loop.

提交回复
热议问题