Adding three months to a date in PHP

前端 未结 9 1088
我寻月下人不归
我寻月下人不归 2020-12-02 13:03

I have a variable called $effectiveDate containing the date 2012-03-26.

I am trying to add three months to this date and have been unsu

相关标签:
9条回答
  • 2020-12-02 13:13

    You need to convert the date into a readable value. You may use strftime() or date().

    Try this:

    $effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
    $effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
    echo $effectiveDate;
    

    This should work. I like using strftime better as it can be used for localization you might want to try it.

    0 讨论(0)
  • 2020-12-02 13:25

    The following should work,Please Try this:

    $effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
    echo $time = date("y/m/d", $effectiveDate);
    
    0 讨论(0)
  • 2020-12-02 13:26

    Following should work

    $d = strtotime("+1 months",strtotime("2015-05-25"));
    echo   date("Y-m-d",$d); // This will print **2015-06-25** 
    
    0 讨论(0)
  • 2020-12-02 13:28

    This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.

    $date = new DateTime('now');
    $date->modify('+3 month'); // or you can use '-90 day' for deduct
    $date = $date->format('Y-m-d h:i:s');
    echo $date;
    
    0 讨论(0)
  • 2020-12-02 13:33

    Add nth Days, months and years

    $n = 2;
    for ($i = 0; $i <= $n; $i++){
        $d = strtotime("$i days");
        $x = strtotime("$i month");
        $y = strtotime("$i year");
        echo "Dates : ".$dates = date('d M Y', "+$d days");
        echo "<br>";
        echo "Months : ".$months = date('M Y', "+$x months");
        echo '<br>';
        echo "Years : ".$years = date('Y', "+$y years");
        echo '<br>';
    }
    
    0 讨论(0)
  • 2020-12-02 13:38

    Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:

    $effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );
    

    (This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)

    0 讨论(0)
提交回复
热议问题