How to increase the number of days in $Date in PHP
P粉741678385
2023-08-20 15:57:20
<p>I have a date returned as part of a MySQL query in the format <code>2010-09-17</code>. </p>
<p>I want to set the variables $Date2 to $Date5 as follows: </p>
<p><code>$Date2 = $Date 1</code></p>
<p><code>$Date3 = $Date 2</code></p>
<p>Wait, this returns <code>2010-09-18</code>, <code>2010-09-19</code>, etc. </p>
<p>I tried</p>
<pre class="brush:php;toolbar:false;">date('Y-m-d', strtotime($Date. ' 1 day'))</pre>
<p>But this returns me the date <em>before </em><code>$Date</code>. </p>
<p>Is there any correct way to get my dates in 'Y-m-d' format so they can be used in another query? </p>
If you are using PHP 5.3, you can use the
DateTime
object and itsadd
method:See the
DateInterval
Constructor manual 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: