Pankaj Khurana Pankaj Khurana - 6 months ago 7
PHP Question

Add number of days to a date

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

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.