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?
Here is a small snippet to demonstrate the date modifications:
If you're using PHP 5.3, you can use a
DateTime
object and itsadd
method:Take a look at the
DateInterval
constructor manual page to see how to construct other periods to add to your date (2 days would be'P2D'
, 3 would be'P3D'
, and so on).Without PHP 5.3, you should be able to use
strtotime
the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):Here has an easy way to solve this.
Output will be:
Solution has found from here - How to Add Days to Date in PHP
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
Be careful when adding months... (and to a lesser extent, years)
All you have to do is use
days
instead ofday
like this:And it outputs correctly:
Using a variable for Number of days