Add number of days to a date

PhpDate

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

This one might be good

function addDayswithdate($date,$days){

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

Solution 3 - Php

$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"));

Solution 4 - Php

$today=date('d-m-Y');
$next_date= date('d-m-Y', strtotime($today. ' + 90 days'));
echo $next_date;

Solution 5 - Php

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

Solution 6 - Php

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;
}

Solution 7 - Php

You could also try:

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

Solution 8 - Php

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"));

Solution 9 - Php

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

Solution 10 - Php

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');

Solution 11 - Php

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.

Solution 12 - Php

$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];

Solution 13 - Php

You may try this.

$i=30;
echo  date("Y-m-d",mktime(0,0,0,date('m'),date('d')+$i,date('Y')));

Solution 14 - Php

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

Solution 15 - Php

//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**

Solution 16 - Php

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

Solution 17 - Php

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

Solution 18 - Php

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.

Solution 19 - Php

//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;

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
QuestionPankaj KhuranaView Question on Stackoverflow
Solution 1 - PhpGordonView Answer on Stackoverflow
Solution 2 - PhpGowriView Answer on Stackoverflow
Solution 3 - PhpchxView Answer on Stackoverflow
Solution 4 - Phpshraddha DharmamerView Answer on Stackoverflow
Solution 5 - PhpAndyView Answer on Stackoverflow
Solution 6 - PhpBjoernView Answer on Stackoverflow
Solution 7 - PhpInsectatoriousView Answer on Stackoverflow
Solution 8 - PhpPaul TruesdellView Answer on Stackoverflow
Solution 9 - PhpLaxman13View Answer on Stackoverflow
Solution 10 - PhpFabrizioView Answer on Stackoverflow
Solution 11 - PhpmqchenView Answer on Stackoverflow
Solution 12 - PhpMayank RaipureView Answer on Stackoverflow
Solution 13 - PhpJ.RobView Answer on Stackoverflow
Solution 14 - Phpuser3767878View Answer on Stackoverflow
Solution 15 - PhpSajan PatelView Answer on Stackoverflow
Solution 16 - PhpJuancho RamoneView Answer on Stackoverflow
Solution 17 - PhpJoyGuruView Answer on Stackoverflow
Solution 18 - PhpDeepak DanielView Answer on Stackoverflow
Solution 19 - PhpDharmendra Verma Web DeveloperView Answer on Stackoverflow