How to increase the number of days in $Date in PHP
P粉741678385
P粉741678385 2023-08-20 15:57:20
0
2
575
<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>
P粉741678385
P粉741678385

reply all(2)
P粉393030917

If you are using PHP 5.3, you can use the DateTime object and its add method:

$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D表示1天的时间段
$Date2 = $date->format('Y-m-d');

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):

$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2)返回"2010-09-18"
P粉258788831

You just need to use days instead of day like this:

<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>

It will correctly output:

2010-09-18
2010-09-19
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template