Add number of days to a date

Pankaj Khurana picture Pankaj Khurana · Feb 25, 2010 · Viewed 380.5k times · Source

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.

Answer

Gordon picture Gordon · Feb 25, 2010

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.