How to increase the number of days in $Date in PHP
P粉741678385
P粉741678385 2023-08-20 15:57:20
0
2
421

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?

P粉741678385
P粉741678385

reply all (2)
P粉393030917

If you are using PHP 5.3, you can use theDateTimeobject and itsaddmethod:

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

See theDateIntervalConstructormanual 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 usestrtotimeas 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 usedaysinstead ofdaylike this:

    It will correctly output:

    2010-09-18 2010-09-19
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!