php - add + 7 days to date format mm dd, YYYY

前端 未结 7 448
野的像风
野的像风 2021-01-31 01:48

I have date of this format March 3, 2011 in database and I need to extend it with 7 days. I mean

 $date = $date + 7
. Is there any build in function to do that ?

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 02:27

    I would solve this like that. First, I'd create an instance of your given datetime object. Then, I'd create another datetime object which is 7 days later than the initial one. And finally, I'd format it the way you like.

    With meringue library, this is quite intuitive and elegant. Here's the code:

    (new Future(
        new FromCustomFormat('F j, Y', 'March 3, 2011'),
        new NDays(7)
    ))
        ->value();
    

    The result is a string in ISO8601 format. If you like, you can format it anyway you like using the same ISO8601 syntax:

    (new ISO8601Formatted(
        new Future(
            new FromCustomFormat('F j, Y', 'March 3, 2011'),
            new NDays(7)
        ),
        'F j, Y'
    ))
        ->value();
    

    The code above uses meringue library. Here's a quick start, you can take a look if you want.

提交回复
热议问题