134

I want to add number of days to current date: I am using following code:

$i=30;
echo $date = strtotime(date("Y-m-d", strtotime($date)) . " +".$i."days");

But instead of getting proper date i am getting this: 2592000

Please suggest.

Pankaj
  • 9,749
  • 32
  • 139
  • 283
Pankaj Khurana
  • 3,243
  • 10
  • 50
  • 79
  • Or http://stackoverflow.com/questions/277247/increase-days-to-php-current-date – Mikel Mar 13 '11 at 02:50
  • Or http://stackoverflow.com/questions/2692679/php-calculating-future-date-by-adding-days-to-a-variable-date – Mikel Mar 13 '11 at 02:51
  • 4
    search using tag names with brackets around them, e.g. `[php] [date] add` in the search box. You will find many answers to questions like this. – Mikel Mar 13 '11 at 02:53

20 Answers20

265

This should be

echo date('Y-m-d', strtotime("+30 days"));

strtotime

expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

while date

Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

See the manual pages for

and their function signatures.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • I think this will cause issues when crossing daylight savings times and other funny calendar items. It sort-of depends upon the context. You need to think about if you are looking for 30 24 hour blocks, which, when crossing a daylight savings time day, might put you at 11:00PM. – JJ Rohrer Jul 23 '14 at 16:00
  • 1
    @JJRohrer no, it won't. `strtotime` will take any DST for the configured timezone into account. DST is only an issue when you do something like this OP: http://stackoverflow.com/questions/4066035 – Gordon Jul 23 '14 at 16:51
  • Point taken. I arrived at this Question trying to solve something a little closer to the question you referenced. I hope my above comment with get others thinking about taking their date calculations seriously - its easy to forgot about all of the exceptions. – JJ Rohrer Jul 24 '14 at 18:28
  • The original question was trying add 30 days to a variable date: $date but this solution adds 30 days to the current date. – php-b-grader Apr 29 '22 at 03:38
  • @php-b-grader the question clearly states "I want to add number of days to *current* date" and the OP accepted it. So not sure your interpretation of the OP's intent holds up. – Gordon Apr 29 '22 at 11:41
68

This one might be good

function addDayswithdate($date,$days){

    $date = strtotime("+".$days." days", strtotime($date));
    return  date("Y-m-d", $date);

}
Gowri
  • 16,587
  • 26
  • 100
  • 160
47
$date = new DateTime();
$date->modify('+1 week');
print $date->format('Y-m-d H:i:s');

or print date('Y-m-d H:i:s', mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 7, date("Y"));

chx
  • 11,270
  • 7
  • 55
  • 129
  • This method addresses crossing daylight savings time. I'm not sure if php accounts for other time quirks, like leap seconds, but, depending upon your needs, you are probably better off using DateTime vs. StrToTime if you dealing with calendar days. – JJ Rohrer Jul 23 '14 at 16:04
  • @chx This solution also more carefully allows a date; e.g., new DateTime('2016-12-09 10:35:58'); that is coming from a database timestamp... – mshaffer Dec 19 '16 at 23:43
11
$today=date('d-m-Y');
$next_date= date('d-m-Y', strtotime($today. ' + 90 days'));
echo $next_date;
10

You can add like this as well, if you want the date 5 days from a specific date :

You have a variable with a date like this (gotten from an input or DB or just hard coded):

$today = "2015-06-15"; // Or can put $today = date ("Y-m-d");

$fiveDays = date ("Y-m-d", strtotime ($today ."+5 days"));

echo $fiveDays; // Will output 2015-06-20
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Andy
  • 101
  • 1
  • 2
7

Keep in mind, the change of clock changes because of daylight saving time might give you some problems when only calculating the days.

Here's a little php function which takes care of that:

function add_days($date, $days) {
    $timeStamp = strtotime(date('Y-m-d',$date));
    $timeStamp+= 24 * 60 * 60 * $days;

    // ...clock change....
    if (date("I",$timeStamp) != date("I",$date)) {
        if (date("I",$date)=="1") { 
            // summer to winter, add an hour
            $timeStamp+= 60 * 60; 
        } else {
            // summer to winter, deduct an hour
            $timeStamp-= 60 * 60;           
        } // if
    } // if
    $cur_dat = mktime(0, 0, 0, 
                      date("n", $timeStamp), 
                      date("j", $timeStamp), 
                      date("Y", $timeStamp)
                     ); 
    return $cur_dat;
}
Bjoern
  • 15,934
  • 4
  • 43
  • 48
7

You could also try:

$date->modify("+30 days");

Insectatorious
  • 1,305
  • 3
  • 14
  • 29
6

I know this is an old question, but for PHP <5.3 you could try this:

$date = '05/07/2013';
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days)); //my preferred method
//or
$date = date('Y-m-d',strtotime($date.' +'.$add_days.' days');
Fabrizio
  • 3,734
  • 2
  • 29
  • 32
6

You can do it by manipulating the timecode or by using strtotime(). Here's an example using strtotime.

$data['created'] = date('Y-m-d H:i:s', strtotime("+1 week"));

Paul Truesdell
  • 329
  • 2
  • 5
6

You can use strtotime()
$data['created'] = date('Y-m-d H:m:s', strtotime('+1 week'));

Laxman13
  • 5,226
  • 3
  • 23
  • 27
5
$date = "04/28/2013 07:30:00";

$dates = explode(" ",$date);

$date = strtotime($dates[0]);

$date = strtotime("+6 days", $date);

echo date('m/d/Y', $date)." ".$dates[1];
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
Mayank Raipure
  • 1,773
  • 15
  • 12
5

You could use the DateTime class built in PHP. It has a method called "add", and how it is used is thoroughly demonstrated in the manual: http://www.php.net/manual/en/datetime.add.php

It however requires PHP 5.3.0.

mqchen
  • 4,195
  • 1
  • 22
  • 21
4

You may try this.

$i=30;
echo  date("Y-m-d",mktime(0,0,0,date('m'),date('d')+$i,date('Y')));
J.Rob
  • 436
  • 1
  • 5
  • 22
3

Simple and Best

echo date('Y-m-d H:i:s')."\n";
echo "<br>";
echo date('Y-m-d H:i:s', mktime(date('H'),date('i'),date('s'), date('m'),date('d')+30,date('Y')))."\n";

Try this

Bikal Basnet
  • 1,639
  • 1
  • 21
  • 31
user3767878
  • 189
  • 2
  • 5
3
//add the two day

$date = "**2-4-2016**"; //stored into date to variable

echo date("d-m-Y",strtotime($date.**' +2 days'**));

//print output
**4-4-2016**
Vural
  • 8,666
  • 11
  • 40
  • 57
2

Use this addDate() function to add or subtract days, month or years (you will need the auxiliar function reformatDate() as well)

/**
 * $date self explanatory
 * $diff the difference to add or subtract: e.g. '2 days' or '-1 month'
 * $format the format for $date
 **/
    function addDate($date = '', $diff = '', $format = "d/m/Y") {
        if (empty($date) || empty($diff))
            return false;
        $formatedDate = reformatDate($date, $format, $to_format = 'Y-m-d H:i:s');
        $newdate = strtotime($diff, strtotime($formatedDate));
        return date($format, $newdate);
    }
    //Aux function
    function reformatDate($date, $from_format = 'd/m/Y', $to_format = 'Y-m-d') {
        $date_aux = date_create_from_format($from_format, $date);
        return date_format($date_aux,$to_format);
    }

Note: only for php >=5.3

Juan Girini
  • 1,150
  • 2
  • 16
  • 31
2

Use the following code.

<?php echo date('Y-m-d', strtotime(' + 5 days')); ?>

Reference has found from here - How to Add Days to Current Date in PHP

JoyGuru
  • 1,803
  • 20
  • 11
2

Even though this is an old question, this way of doing it would take of many situations and seems to be robust. You need to have PHP 5.3.0 or above.

$EndDateTime = DateTime::createFromFormat('d/m/Y', "16/07/2017");
$EndDateTime->modify('+6 days');
echo $EndDateTime->format('d/m/Y');

You can have any type of format for the date string and this would work.

1
//Set time zone
date_default_timezone_set("asia/kolkata");
$pastdate='2016-07-20';
$addYear=1;
$addMonth=3;
$addWeek=2;
$addDays=5;
$newdate=date('Y-m-d', strtotime($pastdate.' +'.$addYear.' years +'.$addMonth. ' months +'.$addWeek.' weeks +'.$addDays.' days'));
echo $newdate;
0

Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.

Use the DateTime class

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

The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.

The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks

Source

Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:

$now = strtotime('31 December 2019');

for ($i = 1; $i <= 6; $i++) {
    echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}

You'd get the following sequence of dates:

31 December
31 November
31 October
31 September
31 August
31 July
31 June

PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:

01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
zanderwar
  • 3,440
  • 3
  • 28
  • 46