10
$date = date('Y-m-d',current_time('timestamp', 0));

How do I change $date to $date + 5 days?

PHP version is 5.2.

This code doesn't work:

$date_cur = date('Y-m-d',current_time('timestamp', 0));
echo $date_cur . ' <br>';
$date_cur_plus = date($date_cur, strtotime('+5 days'));
echo $date_cur_plus;

Gives me:

2011-11-29 
2011-11-29
Jasper
  • 5,090
  • 11
  • 34
  • 41
  • duplicate of all of those http://stackoverflow.com/search?q=add+days+php – Gordon Nov 29 '11 at 09:11
  • possible duplicate of [Add number of days to a date](http://stackoverflow.com/questions/2332681/add-number-of-days-to-a-date) – edorian Nov 29 '11 at 09:13

11 Answers11

40
$date = date('Y-m-d', strtotime('+5 days'));
matino
  • 17,199
  • 8
  • 49
  • 58
19

You could use mktime() using the timestamp.

Something like:

$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y')));

Using strtotime() is faster, but my method still works and is flexible in the event that you need to make lots of modifications. Plus, strtotime() can't handle ambiguous dates.

Edit

If you have to add 5 days to an already existing date string in the format YYYY-MM-DD, then you could split it into an array and use those parts with mktime().

$parts = explode('-', $date);
$datePlusFive = date(
    'Y-m-d', 
    mktime(0, 0, 0, $parts[1], $parts[2] + 5, $parts[0])
    //              ^ Month    ^ Day + 5      ^ Year
);
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
5

Object oriented Style:

<?php
    $date = new DateTime('now');
    $date->add(new DateInterval('P5D'));
    echo $date->format('Y-m-d') . "\n";
?>

Procedural Style:

<?php
    $date = date_create('2016-01-01');
    date_add($date, date_interval_create_from_date_string('5 days'));
    echo date_format($date, 'Y-m-d');
?>
Turdaliev Nursultan
  • 2,538
  • 1
  • 22
  • 28
4

For specific date:

$date = '2011-11-01';
$date_plus = date('Y-m-d', strtotime($date.'+5 days'));
echo $date.'<br>'.$date_plus;

It will be give :

2011-11-01
2011-11-06
4

Use strtotime:

$date = date('Y-m-d', strtotime('+5 days'));
phihag
  • 278,196
  • 72
  • 453
  • 469
3
$dateplus5 = date('Y-m-d', strtotime('+5 days'));
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
2

strtotime() is very nice. It allows you to do the following:

$startDate = 'now'; // or choose a certain date you want/have
$startDate = '2013-02-14';
$intervals = array(
    '', 
    '+ 5 days', 
    '+ 31 days', 
    '+ 3 months', 
    '+ 2 years + 2 days'
); 
foreach($intervals as $interval) {
    $combinedDate = $startDate . ' ' . $interval;
    var_dump($combinedDate . ' => ' date('Y-m-d', strtotime($combinedDate)));
}

with a result:

now => 1360334498 = 2013-02-08

now + 5 days => 1360766498 = 2013-02-13

now + 31 days => 1363012898 = 2013-03-11

now + 3 months => 1368020498 = 2013-05-08

now + 2 years + 2 days => 1423579298 = 2015-02-10

or:

2013-02-14 => 1360792800 = 2013-02-14

2013-02-14 + 5 days => 1361224800 = 2013-02-19

2013-02-14 + 31 days => 1363471200 = 2013-03-17

2013-02-14 + 3 months => 1368478800 = 2013-05-14

2013-02-14 + 2 years + 2 days => 1424037600 = 2015-02-16

Community
  • 1
  • 1
temuraru
  • 1,538
  • 1
  • 10
  • 7
2

You can use

strtotime(“+5 days”)

to get the current date plus 5 days or

$targetDate = date($date, strtotime(’+5 days’));
Taz
  • 1,235
  • 9
  • 16
0

I used this:

$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);

It's working now.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Did not supposed to be like this?

$date_cur = date('Y-m-d', current_time('timestamp', 0));
echo $date_cur . ' <br>';
$date_cur_plus = date('Y-m-d', strtotime('+5 days', current_time('timestamp', 0) ) );
echo $date_cur_plus;
Lafif Astahdziq
  • 3,788
  • 29
  • 38
0

if the date is already exists, you can used this code :

$tartDate = date("m/d/Y", strtotime("+1 Day", strtotime($Date)));
Muh faris
  • 197
  • 2
  • 6