68

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 ?
Michael
  • 6,377
  • 14
  • 59
  • 91
  • 2
    possible duplicate of [Add number of days to a date](http://stackoverflow.com/questions/2332681/add-number-of-days-to-a-date) – Gordon Mar 02 '11 at 23:27
  • What format is the date type in your DB? Your best bet will be to pull the data out and run it through your own function, increment the additional days to the string and then pop it back into the DB. – a.stgeorge Mar 02 '11 at 23:27
  • and many more in http://stackoverflow.com/search?q=add+7+days+date+php – Gordon Mar 02 '11 at 23:28

7 Answers7

159
$date = "Mar 03, 2011";
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • 1
    is there also anyway to compare the final form with the today date ? (e.g if ($date < current date) {echo ' the date is in the past' }? – Michael Mar 02 '11 at 23:59
  • @Michael the result of strtotime is an integer. This integer represents the number of seconds since the UNIX epoch (Jan 1 1970 00:00:00). These integers can be compared using the exact code you just used. – Matthew Scharley Mar 03 '11 at 00:23
  • @Michael the `time()` function can be used to get the current date/time as a UNIX timestamp for comparison. – Matthew Scharley Mar 03 '11 at 00:40
  • yeah you're right .. anyway I used strtotime("now") instead of time() – Michael Mar 03 '11 at 01:05
24

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
Kenshi Mokak
  • 253
  • 2
  • 5
22

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

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100
21

Another more recent and object style way to do it :

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

php doc of datetime add

Nicolas
  • 571
  • 6
  • 13
5

yes

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

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

Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
0
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);
}
RBT
  • 24,161
  • 21
  • 159
  • 240
  • **From review queue**: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 20 '19 at 12:13
0

$date = date('Y-m-d',strtotime("+7 days", strtotime(date('Y-m-d'))));