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.
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.
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.
This one might be good
function addDayswithdate($date,$days){
$date = strtotime(\"+\".$days.\" days\", strtotime($date));
return date(\"Y-m-d\", $date);
}
$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\"));
$today=date(\'d-m-Y\');
$next_date= date(\'d-m-Y\', strtotime($today. \' + 90 days\'));
echo $next_date;
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
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;
}
You could also try:
$date->modify(\"+30 days\");
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\"));
You can use strtotime()
$data[\'created\'] = date(\'Y-m-d H:m:s\', strtotime(\'+1 week\'));
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\');
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.
$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];
You may try this.
$i=30;
echo date(\"Y-m-d\",mktime(0,0,0,date(\'m\'),date(\'d\')+$i,date(\'Y\')));
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
//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**
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
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
//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;
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.