Alex Rivera | Logout

Adding days to $Date in PHP

Asked 2010-09-16T14:23:07.307
292

I have a date returned as part of a MySQL query in the form 2010-09-17.

I would like to set the variables $Date2 to $Date5 as follows:

$Date2 = $Date + 1

$Date3 = $Date + 2

etc., so that it returns 2010-09-18, 2010-09-19, etc.

I have tried

date('Y-m-d', strtotime($Date. ' + 1 day'))

but this gives me the date before $Date.

What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?

Edit
Report

1 Answer

9

You can also use the following format

strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));

You can stack changes this way:

strtotime("+1 day", strtotime("+1 year", strtotime($date)));

Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.

answered 2012-12-14T11:09:41.630

Your Answer