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

PhpDatetime

Php Problem Overview


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 ?

Php Solutions


Solution 1 - Php

$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);

Solution 2 - Php

The "+1 month" issue with strtotime

As noted in several blogs, strtotime() solves the "+1 month" ("next month") issue on days that do not exist in the subsequent month differently than other implementations like for example MySQL.

$dt = date("Y-m-d");
echo date( "Y-m-d", strtotime( "$dt +1 day" ) ); // PHP:  2009-03-04
echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP:  2009-03-31

Solution 3 - Php

echo date('d/m/Y', strtotime('+7 days'));

Solution 4 - Php

Another more recent and object style way to do it :

$date = new DateTime('now');
$date->add(new DateInterval('P7D'));

php doc of datetime add

Solution 5 - Php

yes

$oneweekfromnow = strtotime("+1 week", strtotime("<date-from-db>"));

on another note, why do you have your date in the database like that?

Solution 6 - Php

onClose: function(selectedDate) {

    $("#dpTodate").datepicker("option", "minDate", selectedDate);
    var maxDate = new Date(selectedDate);
    
     maxDate.setDate(maxDate.getDate() + 6); //6 days extra in from date
    
     $("#dpTodate").datepicker("option", "maxDate", maxDate);
}

Solution 7 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael View Question on Stackoverflow
Solution 1 - PhpMatthew ScharleyView Answer on Stackoverflow
Solution 2 - PhpKenshi MokakView Answer on Stackoverflow
Solution 3 - PhpIlludiumPu36View Answer on Stackoverflow
Solution 4 - PhpNicolasView Answer on Stackoverflow
Solution 5 - PhpJan DragsbaekView Answer on Stackoverflow
Solution 6 - Phpuser11528018View Answer on Stackoverflow
Solution 7 - PhpVadim SamokhinView Answer on Stackoverflow