I have a date returned as part of a MySQL query in the format 2010-09-17
.
I want to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date 1
$Date3 = $Date 2
Wait, this returns 2010-09-18
, 2010-09-19
, etc.
I tried
date('Y-m-d', strtotime($Date. ' 1 day'))
But this returns me the date before $Date
.
Is there any correct way to get my dates in 'Y-m-d' format so they can be used in another query?
If you are using PHP 5.3, you can use the
DateTime
object and itsadd
method:See the
DateInterval
Constructormanual page for how to construct other time periods to add to your date (e.g. 2 days for'P2D'
, 3 days for'P3D'
, etc.).If you don't have PHP 5.3, you should be able to use
strtotime
as you did before (I've tested this and it works in both 5.1.6 and 5.2.10):You just need to use
days
instead ofday
like this:It will correctly output: