Adding days to $Date in PHP

前端 未结 9 1952
时光取名叫无心
时光取名叫无心 2020-11-22 09:32

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:

$Dat

相关标签:
9条回答
  • 2020-11-22 10:27

    From PHP 5.2 on you can use modify with a DateTime object:

    http://php.net/manual/en/datetime.modify.php

    $Date1 = '2010-09-17';
    $date = new DateTime($Date1);
    $date->modify('+1 day');
    $Date2 = $date->format('Y-m-d');
    

    Be careful when adding months... (and to a lesser extent, years)

    0 讨论(0)
  • 2020-11-22 10:28

    All you have to do is 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'));
    ?>
    

    And it outputs correctly:

    2010-09-18
    2010-09-19
    
    0 讨论(0)
  • 2020-11-22 10:29

    Using a variable for Number of days

    $myDate = "2014-01-16";
    $nDays = 16;
    $newDate = strtotime($myDate . '+ '.$nDays.'days');
    echo new Date('d/m/Y', $soma); //format new date 
    
    0 讨论(0)
提交回复
热议问题