Increase days to php current Date()

前端 未结 11 1347
予麋鹿
予麋鹿 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:43

    You can also use Object Oriented Programming (OOP) instead of procedural programming:

    $fiveDays = new DateInterval('P5D');
    $today = new DateTime();
    $fiveDaysAgo = $today->sub(fiveDays); // or ->add(fiveDays); to add 5 days
    

    Or with just one line of code:

    $fiveDaysAgo = (new DateTime())->sub(new DateInterval('P5D'));
    

提交回复
热议问题